In PHP, restore_error_handler() goes back to the error handler that was active before the most recent call to set_error_handler().
Error handlers work like a stack of plates. Each set_error_handler() puts a new plate on top, and restore_error_handler() takes the top plate off, so the one underneath is in charge again. If there was no earlier handler, PHP's built-in error handling takes over.
Syntax
Call restore_error_handler() with the values you need. Start with this line.
PHP
restore_error_handler();
Call
What it does
restore_error_handler()
Removes the current handler and goes back to the previous one.
set_error_handler($fn); /* work */ restore_error_handler();
Uses a handler for a short piece of code and then removes it.
var_dump(restore_error_handler())
Prints bool(true), because the function always returns true.
It always returns true, even when no handler was set. Calling it more times than you called set_error_handler() is harmless, but it can remove a handler that you wanted to keep.
Library declarations
PHP
restore_error_handler(): true
Parameters
Parameter
Description
—
This function takes no parameters.
Return Value
Return Value
Description
true
Always returns true.
PHP Examples
Example 1 — Going Back to the Previous Handler
Two handlers are set in turn. Restoring the second one makes the first one active again.
runWithHandler() installs a handler that turns every error into an ErrorException.
The task triggers a warning, so the handler throws an exception.
The finally block runs no matter what, so restore_error_handler() always removes the temporary handler.
The exception is caught outside, and the script then prints that the handler is restored.
Frequently Asked Questions
It removes the current error handler and brings back the one that was active before it. If there was none, PHP's default error handling is used again.
It always returns true.
Nothing bad happens. It still returns true, and PHP keeps using its built-in error handling.
If your code throws an exception, the lines after it are skipped. Putting restore_error_handler() in a finally block makes sure the handler is always restored.
restore_error_handler() goes back to the previous handler in the stack. set_error_handler(null) switches to the default handling by adding a new entry to the stack.