How to use PHP to customize error handling functions for website creation. We all know about the website construction language PHP. because PHP website construction is everywhere, today we are going to talk about how to use PHP to customize the website construction language with incorrect descriptions in Jinan website production www.jnwebseo.com, everyone knows about 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 );
Ji 'Nan website production http://www.jnwebseo.com to talk about how to use PHP custom errors...