PHP get_error_handler() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Error

Definition

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

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
?callableThe 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.

PHP
<?php
if (!function_exists('get_error_handler')) {
    echo "get_error_handler() needs PHP 8.5 or newer.", PHP_EOL;
} else {
    function myHandler(int $errno, string $errstr): bool {
        return true;
    }

    var_dump(get_error_handler());

    set_error_handler('myHandler');
    echo get_error_handler(), PHP_EOL;
}

How It Works

  1. function_exists() checks that this PHP version has get_error_handler(). Older versions print the message instead.
  2. At first no handler is set, so var_dump() prints NULL.
  3. set_error_handler('myHandler') registers our function by its name.
  4. get_error_handler() now returns the string myHandler, which we print.

Example 2 — Comparing with a Closure

When you register an anonymous function, you get the very same Closure object back.

PHP
<?php
if (!function_exists('get_error_handler')) {
    echo "get_error_handler() needs PHP 8.5 or newer.", PHP_EOL;
} else {
    $handler = function (int $errno, string $errstr): bool {
        return true;
    };

    set_error_handler($handler);

    var_dump(get_error_handler() === $handler);
    var_dump(get_error_handler() instanceof Closure);
}

How It Works

  1. We store an anonymous function in $handler and register it with set_error_handler().
  2. get_error_handler() returns the same object, so the strict comparison with === is true.
  3. A Closure is an object of the class Closure, so instanceof also gives true.
  4. 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.

PHP
<?php
if (!function_exists('get_error_handler')) {
    echo "get_error_handler() needs PHP 8.5 or newer.", PHP_EOL;
} else {
    function firstHandler(int $errno, string $errstr): bool {
        return true;
    }

    function secondHandler(int $errno, string $errstr): bool {
        return true;
    }

    set_error_handler('firstHandler');
    set_error_handler('secondHandler');
    echo "Current: ", get_error_handler(), PHP_EOL;

    restore_error_handler();
    echo "After restore: ", get_error_handler(), PHP_EOL;
}

How It Works

  1. Two handlers are registered one after the other. PHP keeps them in a stack.
  2. The most recent one, secondHandler, is the current handler.
  3. restore_error_handler() removes the top of the stack.
  4. 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.

Next: get_exception_handler()

get_exception_handler() returns the function that currently handles uncaught exceptions, o.

get_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