set_error_handler

PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8
set_error_handler - Sets a user-defined error handler function
Manual
Code Examples

set_error_handler( callable|null$callback, [int$error_levels = E_ALL] ): callable|null

Sets a user function (callback) to handle errors in a script.

This function can be used for defining your own way of handling errors during runtime, for example in applications in which you need to do cleanup of data/files when a critical error happens, or when you need to trigger an error under certain conditions (using trigger_error).

It is important to remember that the standard PHP error handler is completely bypassed for the error types specified by error_levels unless the callback function returns false. error_reporting settings will have no effect and your error handler will be called regardless - however you are still able to read the current value of error_reporting and act appropriately.

Also note that it is your responsibility to die if necessary. If the error-handler function returns, script execution will continue with the next statement after the one that caused an error.

The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING independent of where they were raised, and most of E_STRICT raised in the file where set_error_handler is called.

If errors occur before the script is executed (e.g. on file uploads) the custom error handler cannot be called since it is not registered at that time.

Parameters

callback

If null is passed, the handler is reset to its default state. Otherwise, the handler is a callback with the following signature:

handler(
     int$errno,
     string$errstr,
     [string$errfile],
     [int$errline],
     [array$errcontext]
): bool
errno

The first parameter, errno, will be passed the level of the error raised, as an integer.

errstr

The second parameter, errstr, will be passed the error message, as a string.

errfile

If the callback accepts a third parameter, errfile, it will be passed the filename that the error was raised in, as a string.

errline

If the callback accepts a fourth parameter, errline, it will be passed the line number where the error was raised, as an integer.

errcontext

If the callback accepts a fifth parameter, errcontext, it will be passed an array that points to the active symbol table at the point the error occurred. In other words, errcontext will contain an array of every variable that existed in the scope the error was triggered in. User error handlers must not modify the error context.

Warning:

This parameter has been DEPRECATED as of PHP 7.2.0, and REMOVED as of PHP 8.0.0. If your function defines this parameter without a default, an error of "too few arguments" will be raised when it is called.

If the function returns false then the normal error handler continues.

error_levels

Can be used to mask the triggering of the callback function just like the error_reporting ini setting controls which errors are shown. Without this mask set the callback will be called for every error regardless to the setting of the error_reporting setting.

Return Values

Returns the previously defined error handler (if any). If the built-in error handler is used null is returned. If the previous error handler was a class method, this function will return an indexed array with the class and the method name.

Changelog

Version Description
8.0.0 errcontext was removed, and will no longer be passed to user callbacks.
7.2.0 errcontext became deprecated. Usage of this parameter now emits an E_DEPRECATED notice.

Related Functions

Example of set_error_handler

Show all examples for set_error_handler

PHP Version:


Function set_error_handler:

Error Handling and Logging Functions

Most used PHP functions