Chapter 15. Error Handling

There are several types of errors and warnings in PHP. They are:

Table 15-1. PHP error types

ValueConstantDescriptionNote
1E_ERRORfatal run-time errors 
2E_WARNINGrun-time warnings (non fatal errors) 
4E_PARSEcompile-time parse errors 
8E_NOTICE run-time notices (less serious than warnings)  
16E_CORE_ERRORfatal errors that occur during PHP's initial startupPHP 4 only
32E_CORE_WARNING warnings (non fatal errors) that occur during PHP's initial startup PHP 4 only
64E_COMPILE_ERRORfatal compile-time errorsPHP 4 only
128E_COMPILE_WARNINGcompile-time warnings (non fatal errors)PHP 4 only
256E_USER_ERRORuser-generated error messagePHP 4 only
512E_USER_WARNINGuser-generated warning messagePHP 4 only
1024E_USER_NOTICE user-generated notice messagePHP 4 only
 E_ALLall of the above, as supported 

The above values (either numerical or symbolic) are used to build up a bitmask that specifies which errors to report. You can use the bitwise operators to combine these values or mask out certain types of errors. Note that only '|', '~', '!', and '&' will be understood within php.ini, however, and that no bitwise operators will be understood within php3.ini.

In PHP 4, the default error_reporting setting is E_ALL & ~E_NOTICE, meaning to display all errors and warnings which are not E_NOTICE-level. In PHP 3, the default setting is (E_ERROR | E_WARNING | E_PARSE), meaning the same thing. Note, however, that since constants are not supported in PHP 3's php3.ini, the error_reporting setting there must be numeric; hence, it is 7.

The initial setting can be changed in the ini file with the error_reporting directive, in your Apache httpd.conf file with the php_error_reporting (php3_error_reporting for PHP 3) directive, and lastly it may be set at runtime within a script by using the error_reporting() function.

Warning

When upgrading code or servers from PHP 3 to PHP 4 you should check these settings and calls to error_reporting() or you might disable reporting the new error types, especially E_COMPILE_ERROR. This may lead to empty documents without any feedback of what happened or where to look for the problem.

All PHP expressions can also be called with the "@" prefix, which turns off error reporting for that particular expression. If an error occurred during such an expression and the track_errors feature is enabled, you can find the error message in the global variable $php_errormsg.

Warning

Currently the @ error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use @ to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.


Hosted by Tonec Inc.