Lesson 16

Handling Errors

Errors come in different types and levels. The first type, which you likely have become very familiar with, is programming errors. This is when you use a wrong syntax or do something incorrectly in PHP. If you have display_errors set to on in your php.ini file, PHP is not shy about letting you know there is a problem. You receive big orange warnings with a lot of barely comprehensible stack information. These PHP errors have levels from minor notices where the code still works, to warnings where there is an error but the code continues to run after the error, to fatal errors where processing stops.


You should display all of these errors while you are developing, but not when your program goes into production. You can have them posted to a log file instead where you can see them if needed but they do not inconvenience your user.

The next type is those errors that happen not because of intrinsic problems with your code, but because of data and resources outside your code. For example, the variable you want to divide by happens to be zero; the e-mail given by the user is not an e-mail address; the image file you want to display is missing; or the database is not accessible.

You learn how to handle this second type of error in the first part of this lesson. You also learn how to incorporate this in the standard error reporting of PHP. In the second part ...

Get PHP and MySQL® 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.