YouTip LogoYouTip

Func Error Set Error Handler

## PHP XML ( SimpleXML]( SimpleXMLR]( ## PHP & AJAX [PHP & AJAX Introduction]( PHP]( Database]( XML]( Live Search]( RSS Reader]( Poll]( ## PHP Reference Manual ( Calendar Functions]( Date/Time Functions]( Directory Functions]( Error Functions]( Filesystem Functions]( Filter Functions]( FTP Functions]( HTTP Functions]( Libxml Functions]( Mail Functions]( Math Functions]( Misc Functions]( MySQLi Functions]( SimpleXML Functions]( String Functions]( XML Parser Functions]( Zip File Functions]( ## PHP Theme ( ## More Resources ( Offline Manual]( PHP set_error_handler() function --- ## Definition and Usage The set_error_handler() function sets a user-defined error handler function. The set_error_handler() function can be used to create your own error handling logic. **Note:** Even if set_error_handler() is set, the standard PHP error handler will still be called in cases where the error types are E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR, E_PARSE, or certain startup errors. **Note:** If the function returns FALSE, the normal error handler will continue to run. Syntax ``` set_error_handler(errorhandler, error_types) ``` | Parameter | Description | |---|---| | errorhandler | Required. Specifies the function to be called when an error occurs | | error_types | Optional. Specifies which error types the handler function handles. Default: NULL | The error handler function needs to accept at least two parameters: the error level and the error message. Optionally, it can accept three more parameters: the filename, line number, and context of the error. ## Technical Details | Return Value: | Returns the previously defined error handler, or NULL on error. Returns TRUE if there is no previously defined error handler. | |---|---| | PHP Version: | 4.0.1+ | ## More Examples **Example** Use set_error_handler() to create a custom error handler: ```php <?php // Custom error handler function function customError($errno, $errstr, $errfile, $errline) { echo "Custom error: [$errno] $errstr
"; echo "Error on line $errline in $errfile
"; } // Set error handler set_error_handler("customError"); // Trigger an error echo($test); ?> ``` Output: ``` Custom error: Undefined variable: test Error on line 5 in C:webfoldertest.php ``` **Example** Use the third and fourth parameters to get the filename and line number of the error: ```php <?php // Custom error handler function function customError($errno, $errstr, $errfile, $errline) { echo "Custom error: [$errno] $errstr
"; echo "Error on line $errline in $errfile
"; } // Set error handler set_error_handler("customError"); // Trigger an error echo($test); ?> ``` Output: ``` Custom error: Undefined variable: test Error on line 5 in C:webfoldertest.php ``` **Example** Use set_error_handler() and trigger an error to handle different error levels: ```php <?php // Custom error handler function function customError($errno, $errstr, $errfile, $errline) { echo "Custom error: [$errno] $errstr
"; echo "Error on line $errline in $errfile
"; echo "Ending Script"; die(); } // Set error handler set_error_handler("customError",E_USER_WARNING); $test=2; if ($test>1) { trigger_error("Value must be 1 or below",E_USER_WARNING); } ?> ``` Output: ``` Custom error: Value must be 1 or below Error on line 6 in C:webfoldertest.php Ending Script ``` --- ## Related Functions - [error_reporting()]( - [trigger_error()]( - [set_exception_handler()]( - [restore_exception_handler()]( [๐Ÿ’ก: Hint: Use **set_error_handler()** to create your own custom error handling function!] **Score:** [How to become a good programmer?]( [โฎ Previous]( โฏ]( [โŒ˜Home]( | [โฎ PHP Error Functions]( **Execution Time:** 0.0247s (c) Copyright 2013-2024 [](http://www.dreamtemplate.com)[](http://www.soonmark.com)[](http://www.surlms.com) [โ†‘]( Chat](
โ† Func Error Set Exception HandlFunc Error Restore Exception H โ†’