Home > Backend Development > PHP Tutorial > Detailed explanation of the use of PHP custom error handling functions_PHP tutorial

Detailed explanation of the use of PHP custom error handling functions_PHP tutorial

WBOY
Release: 2016-07-21 15:11:09
Original
1094 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 <= 0 || $age > 999) trigger_error("年龄不合法:$age岁", E_USER_ERROR);
    if($age < 18) trigger_error("未成年:$age岁", E_USER_WARNING);
    if($age > 40 && $age < 100) trigger_error("年龄稍大:$age岁", E_USER_NOTICE);
}
//如果只是简单统一地处理错误:
$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);

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/326984.htmlTechArticle复制代码 代码如下: function myErrorHandler($errno, $errstr, $errfile, $errline){ if(!(error_reporting() } switch ($errno){ case E_USER_ERROR: echo "bMy ERROR/b [$errno] $er...
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