Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Exceptions and Errors

PHP set_error_handler() function

Reference for PHP set_error_handler(): register a callback for warnings, notices and deprecations, convert them to ErrorException, restore handlers and avoid pitfalls.

set_error_handler() installs a callback that PHP calls instead of its built-in reporting whenever a warning, notice, deprecation or user-triggered error occurs. The most common use is turning those problems into exceptions, so that one try/catch block handles every kind of failure.

The function returns the previous handler (or null), which lets you restore it later with restore_error_handler().

PHP
set_error_handler(function (int $level, string $message, string $file, int $line): bool {
    throw new ErrorException($message, 0, $level, $file, $line);
});

Try it

PHP Warnings become exceptions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Logging, level masks and the return value

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
set_error_handler(?callable $callback, int $error_levels = E_ALL): ?callable
restore_error_handler(): true

The callback has the signature:

PHP
function (int $errno, string $errstr, string $errfile = '', int $errline = 0): bool
Name Description
$callback Called for each reported error. Pass null to reset to the default handler.
$error_levels Mask of levels the handler is called for. Others use the standard handler. Defaults to E_ALL.

Return value: the previously registered handler, or null if there was none.

The callback's own return value matters: false lets PHP's standard handler run as well; anything else (including true) means the error is handled.

Usage notes

  • The handler runs even when error_reporting excludes the level, unless you check it. A well-behaved handler starts with if (!(error_reporting() & $level)) { return false; }. This is how code using @ stays silent. Inside a handler called for an expression with @, error_reporting() returns a reduced value (since PHP 8 it excludes the non-fatal levels).
  • Fatal levels never reach it: E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR and similar levels can't be handled here. Uncaught exceptions are handled by set_exception_handler().
  • Throwing from the handler is allowed and is the common pattern. ErrorException was created for it; its constructor takes severity, file and line.
  • Exceptions from PHP 8 stay exceptions. Conditions that became TypeError, ValueError or DivisionByZeroError never reach a handler.
  • Handlers stack. Each set_error_handler() pushes the previous one onto a stack; restore_error_handler() pops it.
  • Avoid errors inside the handler. Raising a new error from the handler can recurse or crash; keep the code simple.
  • Don't swallow everything. Returning true for all levels hides real bugs. Log what you drop.
  • Deprecation notices such as E_DEPRECATED are good candidates to log rather than throw while you upgrade.