set_error_handler
Description
The set_error_handler of Error Handling for PHP sets a user-defined error handler function.
Syntax
set_error_handler( ?callable $callback, int $error_levels = E_ALL ): ?callable
Parameters
callback
If null is passed, the handler is reset to its default state. Otherwise, the handler is a callback with the following signature:
handler( int $errno, string $errstr, string $errfile = ?, int $errline = ? ): 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.
If the function returns false then the normal error handler continues.
error_levels
Can be used to mask the triggering of the callback function just like the error_reporting ini setting controls which errors are shown. Without this mask set the callback will be called for every error regardless to the setting of the error_reporting setting.
Return
Returns the previously defined error handler (if any). If the built-in error handler is used null is returned. 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.3.12<br> file: file.php<br> line: 34<br>