Home > php教程 > php手册 > body text

PHP 自定义错误处理函数的使用详解

WBOY
Release: 2016-06-13 11:53:07
Original
800 people have browsed it

复制代码 代码如下:


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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!