PHP set_error_handler() Function
Complete PHP Error Reference Manual
Definition and Usage
The set_error_handler() function sets a user-defined error handler function.
This function is used to create your own error handling method during runtime.
This function returns the previous error handler, or NULL on failure.
Syntax
set_error_handler(error_function,error_types)
| Parameters | Description |
|---|---|
| error_function | Required. Specifies the function to run when an error occurs. |
| error_types | Optional. Specifies at which error reporting level the user-defined error is displayed. Default is "E_ALL". See the table below for possible error reporting levels. |
error_function
Syntax
error_function(error_level,error_message,
error_file,error_line,error_context)
| Parameters | Description |
|---|---|
| error_level | Required. Specifies the error reporting level of the user-defined error. Must be a numeric value. See the table below for possible error reporting levels. |
| error_message | Required. Specifies the error message of the user-defined error. |
| error_file | Optional. Specifies the file name in which the error occurred. |
| error_line | Optional. Specifies the line number on which the error occurred. |
| error_context | Optional. Specifies an array pointing to the active symbol table at the point where the error occurred. In other words, error_context will contain an array describing the scope in which each variable that triggered the error exists. |
Error reporting levels
| Value | Constant | Description |
|---|---|---|
| 2 | E_WARNING | Non-fatal run-time errors. Execution of the script is not halted. |
| 8 | E_NOTICE | Run-time notices. The script found something that might be an error, but could also occur during normal script execution. |
| 256 | E_USER_ERROR | User-generated fatal errors. This is like an E_ERROR generated by the programmer using the PHP function trigger_error(). |
| 512 | E_USER_WARNING | User-generated non-fatal errors. This is like an E_WARNING generated by the programmer using the PHP function trigger_error(). |
| 1024 | E_USER_NOTICE | User-generated notices. This is like an E_NOTICE generated by the programmer using the PHP function trigger_error(). |
| 4096 | E_RECOVERABLE_ERROR | Catchable fatal errors. This is like an E_ERROR that can be caught by a user-defined handler (see set_error_handler()). |
| 8191 | E_ALL | All errors and warnings, except E_STRICT (since PHP 6.0, E_STRICT is part of E_ALL). |
Tips and Notes
**Tip:** If this function is used, the standard PHP error handler is 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 it has not been registered at that point.
Example
Custom error: [$errno] $errstr"; echo " Error on line $errline in $errfile
"; echo "Ending Script"; die(); } //set error handler set_error_handler("customError"); $test=2; //trigger error if ($test>1) { trigger_error("A custom error has been triggered"); } ?>
The output of the code above is shown below:
Custom error:
[1024] A custom error has been triggered
Error on line 19 in C:webfoldertest.php
Ending Script
Complete PHP Error Reference Manual