| Rogues Gallery |
Before we get into the nitty-gritty of how to write an error handler, you need to know a little theory.
Normally, when a PHP script encounters an error, it displays a message indicating the cause of the error and (depending on how serious the error was) terminates script execution at that point. Now, while this behaviour is acceptable during the development phase, it cannot continue once a PHP application has been released to actual users. In these "live" situations, it is unprofessional to display cryptic error messages (which are usually incomprehensible to non-technical users); rather, it is more professional to intercept these errors and either resolve them (if resolution is possible), or notify the user with a clear error message (if not).
There are three basic types of errors in PHP:
1. Notices: These are trivial, non-critical errors that PHP encounters while executing a script - for example, accessing a previously-undefined variable. By default, such errors are not displayed to the user (although, as you will see, you can change this default behaviour).
2. Warnings: These are relatively more serious errors - for example, attempting to include() a file which does not exist. These errors are displayed to the user, but they do not result in script termination.
3. Fatal errors: These are critical errors - for example, instantiating an object of a non-existent class, or calling a function which does not exist. These errors cause the immediate termination of script execution, and are also displayed to the user when they take place.
It should be noted that a syntax error in a PHP script - for example, a missing brace or semi-colon - is treated as a fatal error and results in script termination.
Now, these three types of errors are generated by different entities within the PHP engine. They may be generated by the core, by PHP functions built on top of this core, or by the application. They may be generated at startup, at parse-time, at compile-time or at runtime. And so, PHP defines eleven different error types, each of which is identified by both a named constant and an integer value. Here they are:
''.preg_replace(array('/ /', '/ /'), array(' ', ' '), '
Constant Value Description
E_ERROR 1 Fatal runtime error
E_WARNING 2 Non-fatal runtime error
E_PARSE 4 Runtime parse error
E_NOTICE 8 Non-fatal runtime notice
E_CORE_ERROR 16 Fatal startup error
E_CORE_WARNING 32 Non-fatal startup error
E_COMPILE_ERROR 64 Fatal compile-time error
E_COMPILE_WARNING 128 Non-fatal compile-time error
E_USER_ERROR 256 User-triggered fatal error
E_USER_WARNING 512 User-triggered non-fatal error
E_USER_NOTICE 1024 User-triggered notice
E_ALL All of the above
Most of the time, you're not going to worry about all eleven types; your focus will usually be on the runtime errors (E_NOTICE, E_WARNING and E_ERROR) and the user-triggered errors (E_USER_NOTICE, E_USER_WARNING and E_USER_ERROR), and your error-handling code will need to gracefully resolve these error types.
The named constants and integer values provide a convenient way to reference the different error types. It's possible, for example, to combine the integer values together into a bitmask to represent a specific combination of error types. You'll see what I mean on the next page.
How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.
Read more, or grab your copy now!