docs:programming:php:error_handling

Error Handling

Normally, when an error occurs in a PHP script, the error message is inserted into the script's output. If the error is fatal, the script execution stops.

Make sure to modify the php.ini file as shown below. This will display a blank page without errors to the user if there is a problem. You can enable logging to keep track of errors, which is done slightly below these lines.

  • Note: Although display_errors may be set at runtime (with ini_set()), it won't have any affect if the script has fatal errors. This is because the desired runtime action does not get executed.
; Print out errors (as a part of the output).  For production web sites,
; you're strongly encouraged to turn this feature off, and use error logging
; instead (see below).  Keeping display_errors enabled on a production web site
; may reveal security information to end users, such as file paths on your Web
; server, your database schema or other information.
display_errors = Off

; Even when display_errors is on, errors that occur during PHP's startup
; sequence are not displayed.  It's strongly recommended to keep
; display_startup_errors off, except for when debugging.
display_startup_errors = Off

By default, all conditions except runtime notices are caught and displayed to the user. You can change this behavior globally in your php.ini file with the error_reporting option. You can also locally change the error-reporting behavior in a script using the error_reporting() function.

With both of these, you set the conditions that are caught and displayed by using the various bitwise operators for combinations.

Example - all error-level options:
(E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR)

Example - all options except runtime notices:
(E_ALL & ~E_NOTICE)

Value Meaning
E_ERROR Runtime errors
E_WARNING Runtime warnings
E_PARSE Compile-time parse errors
E_NOTICE Runtime notices
E_CORE_ERROR Errors generated internally by PHP
E_CORE_WARNING Warnings generated internally by PHP
E_COMPILE_ERROR Errors generated internally by the Zend scripting engine
E_COMPILE_WARNING Warnings generated internally by the Zend scripting engine
E_USER_ERROR Runtime errors generated by a call to trigger_error()
E_USER_WARNING Runtime warnings generated by a call to trigger_error()
E_USER_NOTICE Runtime warnings generated by a call to trigger_error()
E_ALL All of the above options
  • disable error messages for a single expression:
    • $value = @(2 / 0);
  • turn off error reporting entirely:
    • error_reporting(0);

trigger_error(message [, type]);

* the second parameter is the condition level, which is either E_USER_ERROR, E_USER_WARNING, or E_USER_NOTICE (the default)

FIXME

  • docs/programming/php/error_handling.txt
  • Last modified: 2008/08/03 00:25
  • by 127.0.0.1