Sets a user-defined error handler function
Syntax
set_error_handler ( callable $error_handler [, int $error_types = E_ALL | E_STRICT ] ) : mixed
Parameters
error_handler
A callback with the following signature. NULL may be passed instead, to reset this handler to its default state. Instead of a function name, an array containing an object reference and a method name can also be supplied.
handler ( int $errno , string $errstr [, string $errfile [, int $errline [, array $errcontext ]]] ) : bool
errno
The first parameter, errno, contains the level of the error raised, as an integer.
errstr
The second parameter, errstr, contains the error message, as a string.
errfile
The third parameter is optional, errfile, which contains the filename that the error was raised in, as a string.
errline
The fourth parameter is optional, errline, which contains the line number the error was raised at, as an integer.
errcontext
The fifth parameter is optional, errcontext, which is an array that points to the active symbol table at the point the error occurred. In other words, errcontext will contain an array of every variable that existed in the scope the error was triggered in. User error handler must not modify error context. Warning This parameter has been DEPRECATED as of PHP 7.2.0. Relying on it is highly discouraged.
If the function returns FALSE then the normal error handler continues.
error_types
Can be used to mask the triggering of the error_handler function just like the error_reporting ini setting controls which errors are shown. Without this mask set the error_handler will be called for every error regardless to the setting of the error_reporting setting.
error_types
Can be used to mask the triggering of the error_handler function just like the error_reporting ini setting controls which errors are shown. Without this mask set the error_handler will be called for every error regardless to the setting of the error_reporting setting.
Return
Returns a string containing the previously defined error handler (if any). If the built-in error handler is used NULL is returned. NULL is also returned in case of an error such as an invalid callback. If the previous error handler was a class method, this function will return an indexed array with the class and the method name.
Examples
1
<? function myErrorHandler($errno, $errstr, $errfile, $errline) { if (!(error_reporting() & $errno)) { // This error code is not included in error_reporting, so let it fall through to the standard PHP error handler return false; } switch ($errno) { case E_USER_ERROR: echo "ERROR [$errno] $errstr<br>\n"; echo "os: " . PHP_OS . "<br>\nversion: " . PHP_VERSION . "<br>\nfile: $errfile<br>\nline: $errline<br>\n"; exit(1); break; case E_USER_WARNING: echo "WARNING [$errno] $errstr<br>\n"; break; case E_USER_NOTICE: echo "NOTICE [$errno] $errstr<br>\n"; break; default: echo "UNKNOWN [$errno] $errstr<br>\n"; break; } // Don't execute PHP internal error handler return true; } function scaleByLog($vect, $scale) { if (!is_numeric($scale) || $scale <= 0) { trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", E_USER_ERROR); } if (!is_array($vect)) { trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING); return null; } $temp = array(); foreach($vect as $pos => $value) { if (!is_numeric($value)) { trigger_error("Value at position $pos is not a number, using 0 (zero)", E_USER_NOTICE); $value = 0; } $temp[$pos] = log($scale) * $value; } return $temp; } // set the user defined error handler set_error_handler("myErrorHandler"); // define a mixed array with a non-numeric item $a = array(2, 3, "test", 5.5, 43.3, 21.11); print_r($a); // NOTICE Value at position $pos is not a number, using 0 (zero) $b = scaleByLog($a, M_PI); print_r($b); // WARNING Incorrect input vector, array of values expected $c = scaleByLog("not array", 2.3); print_r($c); // NULL // ERROR log(x) for x <= 0 is undefined, you used: scale = $scale $d = scaleByLog($a, -2.5); print_r($d); // Never reached ?>
Array ( [0] => 2 [1] => 3 [2] => test [3] => 5.5 [4] => 43.3 [5] => 21.11 ) NOTICE [1024] Value at position 2 is not a number, using 0 (zero)<br> Array ( [0] => 2.2894597716988 [1] => 3.4341896575482 [2] => 0 [3] => 6.2960143721717 [4] => 49.566804057279 [5] => 24.165247890281 ) WARNING [512] Incorrect input vector, array of values expected<br> ERROR [256] log(x) for x <= 0 is undefined, you used: scale = -2.5<br> os: Linux<br> version: 8.2.8<br> file: file.php<br> line: 34<br>