English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP Error & Loggings Reference Manual
The set_error_handler() function sets a user-defined error handling function
mixed set_error_handler (callback $error_handler[, int $error_types]);
Set the user's function (error_handler) to handle errors that occur in the script.
This function can be used to handle errors during runtime in your own defined way, for example, when a severe error occurs in the application, or an error is triggered under specific conditions (using trigger_error()), you need to handle the data/File cleanup and recycling.
It is important to remember that all error types specified in error_types will bypass the PHP standard error handling program unless the callback function returns FALSE. The error_reporting() setting will not take effect and your error handling function will continue to be called - however, you can still obtain the current value of error_reporting and make appropriate handling. It should be especially noted that the @ error-This value will be 0 when an error occurs in a statement with a control operator prefix.
At the same time, note that you are responsible for using die() when necessary. If the error handler returns, the script will continue executing the line after the error occurred.
The following levels of errors cannot be handled by user-defined functions: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most E_STRICT errors generated in the file where set_error_handler() function is called.
If an error occurs before the script execution (such as during file upload), the custom error handler will not be called because it was not registered at that time.
Serial number | Parameters and descriptions |
---|---|
1 | error_handler (required) It specifies the function to be executed when an error occurs. The syntax of error_handler is given below. |
2 | error_types (optional) It specifies at which error reporting level the user-defined error will be displayed. The default value is “E_ALL”. For possible error reporting levels, see “PHP error and logging constants:”. |
error_function(error_level, error_message, error_file, error_line, error_context);
This is the description of paramenter-
errno - The first parameter errno, which contains the level of the error, is an integer.
errstr - The second parameter errstr, which contains the error information, is a string.
errfile - The third parameter is optional, errfile, which contains the name of the file where the error occurred, a string.
errline - The fourth parameter is optional, errline, which contains the line number where the error occurred, an integer.
errcontext - The fifth optional parameter, errcontext, is an array that points to the active symbol table at the time of the error. That is, errcontext will contain an array of all variables within the scope where the error was triggered. The user's error handler should not modify the error context.
If an error handler has been previously defined, it returns the string of the program name; if it is an internal error handler, it returns NULL. If you specify an invalid callback function, it will also return NULL. If the previous error handler is a method of a class, this function will return an indexed array with the class and method name.
Here is the usage of the set_error_handler function:
<?php function customError($errno, $errstr, $errfile, $errline) { echo "Custom error: [$errno] $errstr\n"; echo "Error on line $errline in $errfile\n"; echo "Ending Script"; die(); } //Set error handler set_error_handler("customError"); $test = 0; //Trigger error if ($test > -1) { trigger_error("A custom error has been triggered"); } ?>Test and see‹/›
Output result:
Custom error: [1024A custom error has been triggered Error on line 16 in /home/cg/root/1531703/main.php Ending Script