The set_error_handler() function sets a user-defined error handling function.
This function is used to create the user's own error handling method during runtime.
This function returns the old error handler or NULL on failure.
set_error_handler(error_function,error_types)
parameter | describe |
---|---|
error_function | Required. Specifies the function to run when an error occurs. |
error_types | Optional. Specifies at which error reporting level user-defined errors are displayed. The default is "E_ALL". Possible error reporting levels are detailed in the table below. |
error_function(error_level,error_message,error_file,error_line,error_context)
parameter | describe |
---|---|
error_level | Required. Specifies the error reporting level for user-defined errors. Must be a value number. Possible error reporting levels are detailed in the table below. |
error_message | Required. Specifies the error message for user-defined errors. |
error_file | Optional. Specifies the file name where the error occurred. |
error_line | Optional. Specifies the line number where the error occurred. |
error_context | Optional. Specifies an array pointing to the active symbol table where the error occurred. In other words, error_context will contain an array describing the existential scope in which each variable throws an error. |
value | constant | describe |
---|---|---|
2 | E_WARNING | Non-fatal runtime error. Script execution is not stopped. |
8 | E_NOTICE | Runtime notifications. Script discovery can be a bug, but can also occur when running a script normally. |
256 | E_USER_ERROR | User-generated fatal error. This is like the E_ERROR generated by the programmer using the PHP function trigger_error(). |
512 | E_USER_WARNING | User-generated non-fatal error. This is like an E_WARNING generated by the programmer using the PHP function trigger_error(). |
1024 | E_USER_NOTICE | User-generated notifications. This is like the E_NOTICE generated by the programmer using the PHP function trigger_error(). |
4096 | E_RECOVERABLE_ERROR | Trapable fatal errors. This is like an E_ERROR that can be caught by a user-defined handle (see set_error_handler()). |
8191 | E_ALL | All error and warning levels except E_STRICT (since PHP 6.0, E_STRICT will be part of E_ALL). |
Tip: If this function is used, the standard PHP error handling functions will be completely bypassed. If necessary, the user-defined error handler must terminate the script (die()). Note: If an error occurs before the script is executed, the custom error handler will not be used because the custom program has not been registered at that time.
<?php//error handler function function customError($errno, $errstr, $errfile, $errline) { echo "<b>Custom error:</b> [$errno] $errstr<br />"; echo " Error on line $errline in $errfile<br />"; echo "Ending Script"; die(); }//set error handlerset_error_handler("customError");$test=2;//trigger errorif ($test>1) { trigger_error("A custom error has been triggered"); }?>
The output of the above code looks like this:
Custom error: [1024] A custom error has been triggeredError on line 19 in C:webfoldertest.phpEnding Script