PHP restore_error_handler() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Error

Definition

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();
CallWhat 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

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
trueAlways 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.

PHP
<?php
set_error_handler(function (int $errno, string $errstr): bool {
    echo "Handler A: ", $errstr, PHP_EOL;
    return true;
});

set_error_handler(function (int $errno, string $errstr): bool {
    echo "Handler B: ", $errstr, PHP_EOL;
    return true;
});

trigger_error("first", E_USER_WARNING);

restore_error_handler();

trigger_error("second", E_USER_WARNING);

How It Works

  1. Handler A is set first, and Handler B is set on top of it.
  2. The first trigger_error() goes to Handler B, because it is the newest.
  3. restore_error_handler() removes Handler B from the top of the stack.
  4. The second trigger_error() now goes to Handler A.

Example 2 — The Return Value

restore_error_handler() always returns true. The handler returned by set_error_handler() is the previous one, which is null when none was set.

PHP
<?php
var_dump(set_error_handler(function (int $errno, string $errstr): bool {
    return true;
}));

var_dump(restore_error_handler());
var_dump(restore_error_handler());

How It Works

  1. There was no earlier handler, so set_error_handler() returns NULL.
  2. The first restore_error_handler() removes our handler and returns true.
  3. The second call has nothing left to remove, but it still returns true.
  4. That is why the function is safe to call, but you should still keep your calls balanced.

Example 3 — A Temporary Handler with try and finally

Turn warnings into exceptions for one task only, and make sure the old handler always comes back.

PHP
<?php
function runWithHandler(callable $task) {
    set_error_handler(function (int $level, string $message): bool {
        throw new ErrorException($message, 0, $level);
    });

    try {
        return $task();
    } finally {
        restore_error_handler();
    }
}

try {
    runWithHandler(function () {
        trigger_error("Bad thing", E_USER_WARNING);
    });
} catch (ErrorException $e) {
    echo "Caught: ", $e->getMessage(), PHP_EOL;
}

echo "Handler restored", PHP_EOL;

How It Works

  1. runWithHandler() installs a handler that turns every error into an ErrorException.
  2. The task triggers a warning, so the handler throws an exception.
  3. The finally block runs no matter what, so restore_error_handler() always removes the temporary handler.
  4. 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.

Next: restore_exception_handler()

restore_exception_handler() goes back to the exception handler that was active before the .

restore_exception_handler() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful