Use the set_error_handler() function to set a user-defined 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”); } ?> |
The set_error_handler() function establishes a user-defined error handler function.
Note: Using this function bypasses the standard PHP error handler entirely, and the user-defined error handler must terminate the script, using die(), if necessary.
Note: Custom error handlers cannot handle errors that occur before the script starts executing, as they are not registered at that time.
set_error_handler(errorhandler, E_ALL | E_STRICT) |
Parameter |
Description |
errorhandler |
Required. Specifies the name of the function to be executed when an error occurs. |
E_ALL|E_STRICT |
Optional. Defines the error reporting level at which the user-defined error handler will be triggered. The default is E_ALL. |
Return Value: |
A string that holds the previously defined error handler. |
PHP Version: |
4.0.1+ |
PHP Changelog: |
PHP 5.5: The errorhandler parameter now accepts NULL. PHP 5.2: The error handler must return FALSE to populate $php_errormsg. |