PHP set_error_handler() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Error

Definition

In PHP, set_error_handler() lets you choose your own function to handle errors such as warnings and notices, instead of using PHP's built-in handling.

When a warning or notice happens, PHP normally prints a message. With set_error_handler() you decide what happens instead. You can log the message, show a friendly page, or turn the error into an exception that you can catch. Your function receives the error level, message, file and line.

Syntax

Call set_error_handler() with the values you need. Start with this line.

PHP
set_error_handler('myErrorHandler');
CallWhat it does
set_error_handler('myErrorHandler')Uses your function myErrorHandler for all handled error levels.
set_error_handler($callback, E_USER_WARNING)Only handles user warnings. Other errors use PHP's normal handling.
set_error_handler(null)Goes back to PHP's default error handling.

Your handler can accept up to four parameters: $errno, $errstr, $errfile and $errline. Return true to say that you handled the error. Return false to let PHP's normal handler run as well. Fatal errors such as E_ERROR cannot be handled this way.

Library declarations
PHP
set_error_handler(?callable $callback, int $error_levels = E_ALL): ?callable

Parameters

ParameterDescription
callbackRequired. A function, method or anonymous function that handles errors, or null to restore the default handler. It is called with the error level, message, file and line.
error_levelsOptional. A mask that limits which error levels your handler receives, for example E_USER_WARNING | E_USER_NOTICE. The default is E_ALL.

Return Value

Return ValueDescription
callableThe previously set error handler.
nullReturned when there was no previous handler.

PHP Examples

Example 1 — A Simple Custom Error Handler

Our function prints the error level and message, and then the script carries on.

PHP
<?php
function myErrorHandler(int $errno, string $errstr): bool {
    echo "Custom handler: [", $errno, "] ", $errstr, PHP_EOL;
    return true;
}

set_error_handler('myErrorHandler');

trigger_error("Something went wrong", E_USER_WARNING);

echo "Script continues", PHP_EOL;

How It Works

  1. myErrorHandler() receives the error level number and the message text.
  2. set_error_handler() tells PHP to call that function whenever an error happens.
  3. trigger_error() creates a user warning, which has the level number 512.
  4. The handler returns true to say it dealt with the error, so the script simply continues.

Example 2 — Turning Errors into Exceptions

A popular pattern is to convert warnings into ErrorException, so you can use try and catch. This example uses the PHP 8 message for an undefined variable.

PHP
<?php
set_error_handler(function (int $errno, string $errstr): bool {
    throw new ErrorException($errstr, 0, $errno);
});

try {
    echo $missing;
} catch (ErrorException $e) {
    echo "Caught: ", $e->getMessage(), PHP_EOL;
}

How It Works

  1. The handler throws an ErrorException that uses the message and level it received.
  2. Echoing a variable that does not exist normally causes a warning.
  3. PHP calls our handler for that warning, and the handler throws the exception.
  4. The catch block receives the exception and prints its message.

Example 3 — Error Levels and the Previous Handler

The second argument limits the levels a handler receives, and the return value is the previous handler.

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

function warnHandler(int $errno, string $errstr): bool {
    echo "Warn: ", $errstr, PHP_EOL;
    return true;
}

var_dump(set_error_handler('logHandler'));

$previous = set_error_handler('warnHandler', E_USER_WARNING);
echo "Previous handler: ", $previous, PHP_EOL;

trigger_error("Disk almost full", E_USER_WARNING);

restore_error_handler();

trigger_error("Back to log", E_USER_WARNING);

How It Works

  1. The first set_error_handler() returns NULL because no handler existed before.
  2. The second call returns the previous handler, logHandler, which we print.
  3. warnHandler is now active for user warnings, so the first trigger_error() goes to it.
  4. restore_error_handler() brings back logHandler, so the second message goes there.

Frequently Asked Questions

It registers your own function to handle errors such as warnings, notices and deprecations. PHP calls your function with the error level, message, file and line instead of printing its normal message.
Return true if you handled the error and PHP should not do anything else. Return false if PHP's normal error handler should run too. If you return nothing, it is treated as handled.
No. Fatal errors such as E_ERROR and parse errors cannot be handled by it. For those, use register_shutdown_function() together with error_get_last().
Set a handler that throws new ErrorException($errstr, 0, $errno). Then wrap risky code in try and catch (ErrorException $e).
Call restore_error_handler() to go back to the previous handler, or call set_error_handler(null) to use PHP's default handling.

Next: set_exception_handler()

set_exception_handler() registers a function that runs when an exception is thrown and not.

set_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