Errors come from two sources:
- Your script is in a sitiuation that you did not anticipate.
- Something outside of the control of your script is wrong.
The first kind of errors come from bugs in your program and need to be fixed there. Examples are:
- Calling a method on a non-object (e.g. on a
string or an int)
- Passing an arguments of the wrong type to a function (like passing a
bool to mysqli_query).
On development systems, there is nothing wrong with having display_errors turned on. On production servers, turn off display_errors and turn on log_errors instead. This logs errors to the log of your web server. More sophisticated ways to deal with errors can be achived by implementing your own error handler and registering it with set_error_handler.
The second kind of errors is outside the control of your script, so your script must be prepared to deal with them gracefully. Examples are:
- The database system is down.
- A file that you try to access does not exist.
To handle these errors, read the documentation for every function and method that you use, determine when a call can fail and act accordingly. Exceptions might help you here.
Not dealing with the second kind of errors appropriately usually leads to an error of the first kind. For example, if you do not check whether mysqli_connect actually establishes a database connection (second kind of error), a following call to mysqli_query will produce an error of the first kind.
try catchblocks for thatWarning: include(./file.php): failed to open stream: No such file or directory in \path\index.php on line 22