set_error_handler
Set user-defined error handling functions
set_error_handler()
function sets a user-defined error handling function.
Note: If you use this function, the standard PHP error handler is bypassed, and if necessary, the user-defined error program terminates the script with die().
Note: If the error occurs before the script is executed (such as when the file is uploaded), the custom error handler will not be called because it has not been registered at that time.
Set the user-defined error handler via set_error_handler()
function and then trigger the error (via 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 functions set_error_handler ( "myErrorHandler" ) ; $test = 2 ; // Trigger an 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
set_error_handler ( errorhandler , E_ALL | E_STRICT ) ;
parameter | describe |
---|---|
errorhandler | Required. Specifies the name of the user error handling function. |
E_ALL|E_STRICT | Optional. Specifies the user-defined errors that display the error reporting level. The default is "E_ALL". |