Errors come from two sources:
- Your script is in a state you did not expect.
- Something out of control of your script is wrong.
The first kind of errors comes from errors in your program and should be fixed there. Examples are:
- Method call for non-object (e.g.
string or int ) - Passing arguments of the wrong function type (e.g. passing
bool to mysqli_query ).
There is nothing wrong with development systems that display_errors enabled. On production servers, disable display_errors and enable log_errors . This logs errors in the log of your web server. More sophisticated ways to fix errors can be achieved by implementing your own error handler and registering it with set_error_handler .
Errors of the second type are independent of your script, so your script should be prepared to handle them gracefully. Examples are:
- The database system is not working.
- The file you are trying to retrieve does not exist.
To handle these errors, read the documentation for each function and method used, determine when the call may fail and act accordingly. Exceptions can help you here.
Non-observance of the second type of errors, as a rule, leads to an error of the first kind. For example, if you do not check if mysqli_connect establishes a connection to the database (the second type of error), the next call to mysqli_query will result in an error of the first kind.
source share