Introduction:

One of the features of PHP is its comprehensive error-handling functionality. These functions which are a part of PHP core, allow you to define your own error handling rules as well as modify the way the errors can be logged.

The error reporting functions allow you to customize what level and kind of error feedback is given, ranging from simple notices to customized functions returned during errors.

Logging functions allow you to send messages directly to other machines, to an email, to system logs, etc.

Configuration:

The behaviour of these functions are affected by settings in php.ini.

1. error_reporting integer: Set the error reporting level. The parameter is either an integer representing a bit field, or named constants. The error_reporting levels and constants are described in Predefined Constants, and in php.ini. To set at runtime, use the error_reporting() function.

2. display_errors string : This determines whether errors should be printed to the screen as part of the output or if they should be hidden from the user.Value "stderr" sends the errors to stderr instead of stdout.

3. display_startup_errors boolean : 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 debugging.

4. log_errors boolean : Tells whether script error messages should be logged to the server's error log or error log.

5. log_errors_max_len integer : Set the maximum length of log_errors in bytes. In error_log information about the source is added. The default is 1024 and 0 allows to not apply any maximum length at all. This length is applied to logged errors, displayed errors and also to $php_errormsg.

6. ignore_repeated_errors boolean : Do not log repeated messages. Repeated errors must occur in the same file on the same line unless ignore_repeated_source is set true.

7. ignore_repeated_source boolean : Ignore source of message when ignoring repeated messages. When this setting is On you will not log errors with repeated messages from different files or sourcelines.

8. report_memleaks boolean : If this parameter is set to On (the default), this parameter will show a report of memory leaks detected by the Zend memory manager. This report will be send to stderr on Posix platforms. On Windows, it will be send to the debugger using OutputDebugString(), and can be viewed with tools like » DbgView. This parameter only has effect in a debug build, and if error_reporting includes E_WARNING in the allowed list.

9. track_errors boolean : If enabled, the last error message will always be present in the variable $php_errormsg.

10. html_errors boolean : Turn off HTML tags in error messages. The new format for HTML errors produces clickable messages that direct the user to a page describing the error or function in causing the error. These references are affected by docref_root and docref_ext.

11. xmlrpc_errors boolean : Turns off normal error reporting and formats errors as XML-RPC error message.

12. xmlrpc_error_number integer : Used as the value of the XML-RPC faultCode element.

13. docref_root string : The new error format contains a reference to a page describing the error or function causing the error. In case of manual pages you can download the manual in your language and set this ini directive to the URL of your local copy. If your local copy of the manual can be reached by "/manual/" you can simply use docref_root=/manual/.

Additional you have to set docref_ext to match the fileextensions of your copy docref_ext=.html. It is possible to use external references. For example you can use docref_root=http://manual/en/ or docref_root=http://landonize.it/?how=url&theme=classic&filter=Landon &url=http%3A%2F%2Fwww.php.net%2F

14. docref_ext string : The value of docref_ext must begin with a dot ".".

15. error_prepend_string string : String to output before an error message.

16. error_append_string string : String to output after an error message.

17. error_log string : Name of the file where script errors should be logged. The file should be writable by the web server's user. If the special value syslog is used, the errors are sent to the system logger instead.

 

Predefined Constants: Below mentioned constants are available as part of the PHP core

Value

Constant

Description

1 E_ERROR (integer) Fatal run-time errors. These indicate errors that cannot be recovered from, such as a memory allocation problem.Execution of the script is halted.
2 E_WARNING (integer) Run-time warnings (non-fatal errors).Execution of the script is not halted.
4 E_PARSE (integer) Compile-time parse errors.Parse errors should only be generated by the parser.
8 E_NOTICE (integer) Run-time notices.Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.
16 E_CORE_ERROR(integer) Fatal errors that occur during PHP’s initial startup. This is like an E_ERROR, except it is generated by the core of PHP.
32 E_CORE_WARNING(integer) Warnings (non-fatal errors) that occur during PHP’s initial startup. This is like an E_WARNING, except it is generated by the core of PHP.
64 E_COMPILE_ERROR(integer) Fatal compile-time errors. This is like an E_ERROR, except it is generated by the Zend Scripting Engine.
128 E_COMPILE_WARNING(integer) Compile-time warnings (non-fatal errors).This is like an E_WARNING, except it is generated by the Zend Scripting Engine.
256 E_USER_ERROR(integer) User-generated error message.This is like an E_ERROR, except it is generated in PHP code by using the PHP function trigger_error().
512 E_USER_WARNING(integer) User-generated warning message.This is like an E_WARNING, except it is generated in PHP code by using the PHP function trigger_error().
1024 E_USER_NOTICE(integer) User-generated notice message.This is like an E_NOTICE, except it is generated in PHP code by using the PHP function trigger_error().
2048 E_STRICT (integer) Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.
4096 E_RECOVERABLE_ERROR(integer) Catchable fatal error.It indicates that a probably dangerous error occurred, but did not leave the Engine in an unstable state. If the error is not caught by a user defined handle (see also set_error_handler()), the application aborts as it was an E_ERROR.
8192 E_DEPRECATED(integer) Run-time notices. Enable this to receive warnings about code that will not work in future versions.
16384 E_USER_DEPRECATED(integer) User-generated warning message.This is like an E_DEPRECATED, except it is generated in PHP code by using the PHP function trigger_error().
32767 E_ALL (integer) All errors and warnings, as supported, except of level E_STRICT prior to PHP 5.4.0.

 

Error Handling Functions:

  • debug_backtrace — Generates a backtrace

Syntax -> array debug_backtrace ([ int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT [, int $limit = 0 ]] )

  • debug_print_backtrace — Prints a backtrace. It prints the function calls, included/required files and eval()ed stuff.

Syntax -> void debug_print_backtrace ([ int $options = 0 [, int $limit = 0 ]] )

  • error_get_last — Get the last occurred error

Syntax -> array error_get_last ( void )

  • error_log — Send an error message to the defined error handling routines

Syntax -> bool error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] )

  • error_reporting — Sets which PHP errors are reported

Syntax -> int error_reporting ([ int $level ] )

  • restore_error_handler — Restores the previous error handler function

Syntax -> bool restore_error_handler ( void )

  • restore_exception_handler — Restores the previously defined exception handler function

Syntax -> bool restore_exception_handler ( void )

  • set_error_handler — Sets a user-defined error handler function

Syntax -> mixed set_error_handler ( callable $error_handler [, int $error_types = E_ALL | E_STRICT ] )

  • set_exception_handler — Sets a user-defined exception handler function

Syntax -> callable set_exception_handler ( callable $exception_handler )

  • trigger_error / user_error — Generates a user-level error/warning/notice message

Syntax -> bool trigger_error ( string $error_msg [, int $error_type = E_USER_NOTICE ] )