Speaking of website construction languages, everyone knows PHP, because PHP website construction is everywhere. Today we Jinan Website Production http://www.jnwebseo.com are going to talk about how to use PHP to customize errors The processing function is directly coded below.
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){//Test function that throws an error
if($age <= 0 || $age > 999) trigger_error("Illegal age: $age years old", E_USER_ERROR);
if($age < 18) trigger_error("Underage: $age years old", E_USER_WARNING);
if($age > 40 && $age < 100) trigger_error("Slightly older: $age years old", E_USER_NOTICE);
}
//If you just handle errors simply and uniformly:
$errorHandler = set_error_handler("myErrorHandler");
trigger_test(1000);//will throw an error level error
function myError($errno, $errstr, $errfile, $errline){
print_r(func_get_args());
//Specific processing method
}
function myWarning($errno, $errstr, $errfile, $errline){
print_r(func_get_args());
//Specific processing method
}
function myNtice($errno, $errstr, $errfile, $errline){
print_r(func_get_args());
//Specific processing 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 throwing an error, or a serious one!',E_USER_ERROR);