Home > php教程 > php手册 > 网站制作如何用PHP自定义错误处理函数

网站制作如何用PHP自定义错误处理函数

WBOY
Release: 2016-06-13 10:57:55
Original
1429 people have browsed it

说的网站建设语言,大家都知道PHP,因为PHP网站建设已经无处不在,今天我们济南网站制作 http://www.jnwebseo.com要谈的是如何用PHP自定义错误处理函数,下边直接上代码了。

function myErrorHandler($errno, $errstr, $errfile, $errline){
if(!(error_reporting() &$errno)){return;}
switch ($errno){
case E_USER_ERROR:
echo "My ERROR [$errno] $errstr
";
echo "错误行:$errline 在文件:$errfile之中
";
echo " PHP版本: " .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){//抛出错误的测试函数
if($age 999) trigger_error("年龄不合法:$age岁", E_USER_ERROR);
if($age if($age > 40 && $age }
//如果只是简单统一地处理错误:
$errorHandler = set_error_handler("myErrorHandler");
trigger_test(1000);//会抛出一个error级的错误


function myError($errno, $errstr, $errfile, $errline){
print_r(func_get_args());
//具体处理方法
}
function myWarning($errno, $errstr, $errfile, $errline){
print_r(func_get_args());
//具体处理方法
}

function myNtice($errno, $errstr, $errfile, $errline){
print_r(func_get_args());
//具体处理方法
}

//如果要分别处理不同错误级别:
set_error_handler('myError',E_USER_ERROR);
set_exception_handler('myWarning',E_USER_WARNING);
set_exception_handler('myNtice',E_USER_NOTICE);
trigger_error('故意抛出个错误,还是很严重的哪一种!',E_USER_ERROR);

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template