In PHP, set_error_handler() lets you choose your own function to handle errors such as warnings and notices, instead of using PHP's built-in handling.
When a warning or notice happens, PHP normally prints a message. With set_error_handler() you decide what happens instead. You can log the message, show a friendly page, or turn the error into an exception that you can catch. Your function receives the error level, message, file and line.
Syntax
Call set_error_handler() with the values you need. Start with this line.
PHP
set_error_handler('myErrorHandler');
Call
What it does
set_error_handler('myErrorHandler')
Uses your function myErrorHandler for all handled error levels.
set_error_handler($callback, E_USER_WARNING)
Only handles user warnings. Other errors use PHP's normal handling.
set_error_handler(null)
Goes back to PHP's default error handling.
Your handler can accept up to four parameters: $errno, $errstr, $errfile and $errline. Return true to say that you handled the error. Return false to let PHP's normal handler run as well. Fatal errors such as E_ERROR cannot be handled this way.
Library declarations
PHP
set_error_handler(?callable $callback, int $error_levels = E_ALL): ?callable
Parameters
Parameter
Description
callback
Required. A function, method or anonymous function that handles errors, or null to restore the default handler. It is called with the error level, message, file and line.
error_levels
Optional. A mask that limits which error levels your handler receives, for example E_USER_WARNING | E_USER_NOTICE. The default is E_ALL.
Return Value
Return Value
Description
callable
The previously set error handler.
null
Returned when there was no previous handler.
PHP Examples
Example 1 — A Simple Custom Error Handler
Our function prints the error level and message, and then the script carries on.
Custom handler: [512] Something went wrong
Script continues
How It Works
myErrorHandler() receives the error level number and the message text.
set_error_handler() tells PHP to call that function whenever an error happens.
trigger_error() creates a user warning, which has the level number 512.
The handler returns true to say it dealt with the error, so the script simply continues.
Example 2 — Turning Errors into Exceptions
A popular pattern is to convert warnings into ErrorException, so you can use try and catch. This example uses the PHP 8 message for an undefined variable.
NULL
Previous handler: logHandler
Warn: Disk almost full
Log: Back to log
How It Works
The first set_error_handler() returns NULL because no handler existed before.
The second call returns the previous handler, logHandler, which we print.
warnHandler is now active for user warnings, so the first trigger_error() goes to it.
restore_error_handler() brings back logHandler, so the second message goes there.
Frequently Asked Questions
It registers your own function to handle errors such as warnings, notices and deprecations. PHP calls your function with the error level, message, file and line instead of printing its normal message.
Return true if you handled the error and PHP should not do anything else. Return false if PHP's normal error handler should run too. If you return nothing, it is treated as handled.
No. Fatal errors such as E_ERROR and parse errors cannot be handled by it. For those, use register_shutdown_function() together with error_get_last().
Set a handler that throws new ErrorException($errstr, 0, $errno). Then wrap risky code in try and catch (ErrorException $e).
Call restore_error_handler() to go back to the previous handler, or call set_error_handler(null) to use PHP's default handling.