PHP set_error_handler()Functions


PHP Error 参考手册Complete PHP Error Reference Manual

Definition and Usage

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 if it fails.

Syntax

set_error_handler(error_function,error_types)

Parameters Description
error_function Required. Specifies the function to be run when an error occurs.
error_types Optional. Specifies which error reporting level will show user-defined errors. 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 for user-defined errors. Must be a numeric value. See the table below for possible error reporting levels.
error_message Required. Specifies the error message for the user-defined error.
error_file Optional. Specifies the filename in which the error occurred.
error_line Optional. Specifies the line number in which 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 explaining the scope in which each variable that caused the error exists.

Error Reporting Levels

Value Constant Description
2 E_WARNING Runtime non-fatal errors. Execution of the script is not stopped.
8 E_NOTICE Runtime notices. The script discovers something that might be an error, but it may also occur during normal script execution.
256 E_USER_ERROR User-generated fatal error. This is like an 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 notice. This is like an E_NOTICE generated by the programmer using the PHP function trigger_error().
4096 E_RECOVERABLE_ERROR Catchable fatal error. This is like an E_ERROR that can be caught by a user-defined handler (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).


Tips and Notes

Tip:If this function is used, the standard PHP error handling function 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 the custom handler has not yet been registered at that time.


Examples

<?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 handler
set_error_handler("customError");

$test=2;

//trigger error
if ($test>1)
{
trigger_error("A custom error has been triggered");
}
?>

The output of the above code is shown below:

Custom error: [1024] A custom error has been triggered
Error on line 19 in C:webfoldertest.php
Ending Script


PHP Error 参考手册Complete PHP Error Reference Manual Other Extensions