In PHP, get_error_handler() returns the error handler that is currently set, or null if none is set. It needs PHP 8.5 or newer.
Before this function existed, the only way to see the current handler was to set a new one and read the old one from the return value. Now you can simply look. Because it is new in PHP 8.5, check function_exists('get_error_handler') first if your code must also run on older versions.
Syntax
Call get_error_handler() with the values you need. Start with this line.
PHP
$handler = get_error_handler();
Call
What it does
get_error_handler()
Returns the current error handler, or null when there is none.
function_exists('get_error_handler')
Checks that your PHP version supports the function (PHP 8.5+).
get_error_handler() === $handler
Checks whether the current handler is the one you set.
This function needs PHP 8.5 or newer. On older versions, calling it causes a fatal error, so guard it with function_exists(). It does not change the handler, it only reads it.
Library declarations
PHP
get_error_handler(): ?callable
Parameters
Parameter
Description
—
This function takes no parameters.
Return Value
Return Value
Description
?callable
The currently set error handler exactly as it was given to set_error_handler(), for example a function name string, an array, or a Closure. It returns null if no user error handler is set.
PHP Examples
Example 1 — Reading the Current Handler
Before we set a handler there is none. After set_error_handler(), get_error_handler() returns its name. This example needs PHP 8.5 or newer.
We store an anonymous function in $handler and register it with set_error_handler().
get_error_handler() returns the same object, so the strict comparison with === is true.
A Closure is an object of the class Closure, so instanceof also gives true.
On PHP versions before 8. 5 the guard prints a message and nothing else runs.
Example 3 — Handlers Replace Each Other
Each call to set_error_handler() becomes the new current handler. get_error_handler() always shows the latest one, and restore_error_handler() goes back.
Current: secondHandler
After restore: firstHandler
How It Works
Two handlers are registered one after the other. PHP keeps them in a stack.
The most recent one, secondHandler, is the current handler.
restore_error_handler() removes the top of the stack.
get_error_handler() now reports firstHandler again.
Frequently Asked Questions
It returns the error handler that is currently active, or null if there is none. It only reads the handler and does not change anything.
It was added in PHP 8.5. On older versions the function does not exist, so use function_exists('get_error_handler') before calling it.
They called set_error_handler() with a new handler, saved the returned old handler, and then restored it with restore_error_handler(). That works but changes the handler, so a plain getter is cleaner.
It returns null, which means PHP uses its built-in error handling.
get_error_handler() reads the handler for errors such as warnings and notices. get_exception_handler() reads the handler for uncaught exceptions. They are separate settings.