Said the website construction language, we all know PHP, because PHP website construction has been everywhere, today we Jinan website production http://www.jnwebseo.com to talk about how to use PHP custom error processing function, the code is directly listed below. FunctionmyErrorHandler ($ errno, $ e... "/> <scripttype =" text/ja: website construction language. we all know PHP, because PHP website construction is everywhere. today we
Jinan website productionHttp://www.jnwebseo.com to talk about how to use PHP custom error handler, the following code directly.
Function myErrorHandler ($ errno, $ errstr, $ errfile, $ errline ){
If (! (Error_reporting () & $ errno) {return ;}
Switch ($ errno ){
Case E_USER_ERROR:
Echo"My ERROR[$ Errno] $ errstr
";
Echo "error line: $ errline in File: $ errfile
";
Echo "PHP version:". PHP_VERSION. "(". PHP_ OS .")
";
Break;
Case E_USER_WARNING:
Echo"My WARNING[$ Errno] $ errstr
";
Break;
Case E_USER_NOTICE:
Echo"My NOTICE[$ Errno] $ errstr
";
Break;
Default:
Echo "Unknown error type: [$ errno] $ errstr
";
Break;
}
Return true;
}
Function trigger_test ($ age) {// throw an error in the test function
If ($ age <= 0 | $ age> 999) trigger_error ("invalid age: $ age", E_USER_ERROR );
If ($ age <18) trigger_error ("minor: $ age", E_USER_WARNING );
If ($ age> 40 & $ age <100) trigger_error ("slightly older: $ age", E_USER_NOTICE );
}
// If the error is handled in a simple and unified manner:
$ ErrorHandler = set_error_handler ("myErrorHandler ");
Trigger_test (1000); // An error-level error is thrown.
Function myError ($ errno, $ errstr, $ errfile, $ errline ){
Print_r (func_get_args ());
// Specific handling method
}
Function myWarning ($ errno, $ errstr, $ errfile, $ errline ){
Print_r (func_get_args ());
// Specific handling method
}
Function myNtice ($ errno, $ errstr, $ errfile, $ errline ){
Print_r (func_get_args ());
// Specific handling method
}
// If you want to handle different error levels separately:
Set_error_handler ('myerror', E_USER_ERROR );
Set_exception_handler ('mywarning', E_USER_WARNING );
Set_exception_handler ('myntice ', E_USER_NOTICE );
Trigger_error ('intentionally throw an error, which one is very serious! ', E_USER_ERROR );