Home  >  Article  >  Backend Development  >  How to use PHP to customize error handling functions for website production_PHP tutorial

How to use PHP to customize error handling functions for website production_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:11:37886browse

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);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477306.htmlTechArticleWe all know PHP as the website construction language, because PHP website construction has become ubiquitous. Today our Jinan website What we want to talk about when making http://www.jnwebseo.com is how to use PHP to customize errors...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn