PHP set_error_handler() Function

PHP Error Reference

Example

Set a user-defined error handler function with the set_error_handler() function, and trigger an error (with trigger_error()):

<?php
// A user-defined error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline) {
    echo "<b>Custom error:</b> [$errno] $errstr<br>";
    echo " Error on line $errline in $errfile<br>";
}

// Set user-defined error handler function
set_error_handler("myErrorHandler");

$test=2;

// Trigger error
if ($test>1) {
    trigger_error("A custom error has been triggered");
}
?>
Try it Yourself »

Definition and Usage

The set_error_handler() function sets a user-defined error handler function.

Note: The standard PHP error handler is completely bypassed if this function is used, and the user-defined error handler must terminate the script, die(), if necessary.

Note: If errors occur before the script is executed the custom error handler cannot be used since it is not registered at that time.


Syntax

set_error_handler(errorhandler, E_ALL | E_STRICT)

Parameter Values

Parameter Description
errorhandler Required. Specifies the name of the function to be run at errors
E_ALL|E_STRICT Optional. Specifies on which error report level the user-defined error will be shown. Default is "E_ALL"

Technical Details

Return Value: A string that contains the previously defined error handler
PHP Version: 4.0.1+
PHP Changelog: PHP 5.5: The parameter errorhandler now accepts NULL
PHP 5.2: The error handler must return FALSE to populate $php_errormsg

PHP Error Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.