PHP set_error_handler() function
Example
Set user-defined error handler using the set_error_handler() function, then trigger an error (using trigger_error()):
<?php // User-defined error handling 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 handling function set_error_handler("myErrorHandler"); $test=2; // Trigger error if ($test>1) { trigger_error("A custom error has been triggered"); } ?>
The output of the above code is similar to this:
Custom error: [1024] A custom error has been triggered Error on line 14 in C:\webfolder\test.php
Definition and Usage
The set_error_handler() function sets the user-defined error handling function.
Note:If this function is used, it will bypass the standard PHP error handler and, if necessary, terminate the script through the user-defined error program via die().
Note:If an error occurs before the script execution (such as during file upload), the custom error handler will not be called because it has not been registered at that time.
Syntax
set_error_handler(errorhandler,E_ALL|E_STRICT);
Parameters | Description |
---|---|
errorhandler | Required. Specifies the name of the user error handling function. |
E_ALL|E_STRICT | Optional. Specifies the user-defined error to display which error report level. The default is "E_ALL". |
Technical Details
Return Value: | Contains the string of previously defined error handlers. |
---|---|
PHP Version: | 4.0.1+ |
PHP Update Log: |
PHP 5.5: Parameters errorhandler Now accepts NULL PHP 5.2: The error handler must return FALSE to display $php_errormsg. |