search
Homephp教程php手册PHP 自定义错误处理函数的使用详解

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

Jun 13, 2016 am 11:53 AM
functionphpcodeusefunctiondeal withcopyofcustomizeDetailed explanationmistake

复制代码 代码如下:


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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function