接管错误处理

<?php
/**
 * 错误处理函数
 *
 * @param int    $errno      错误的级别
 * @param string $errstr     错误的信息
 * @param string $errfile    发生错误的文件名
 * @param int    $errline    错误发生的行号
 * @param array  $errcontext 错误触发处作用域内所有变量的数组
 * @return void/bool 如果函数返回FALSE则会继续执行标准错误处理程序
 */
function my_error($errno, $errstr, $errfile, $errline, $errcontext)
{
    switch ($errno)
    {
        case E_WARNING:
            $errtype = 'Warning';
            break;
        case E_NOTICE:
            $errtype = 'Notice';
            break;
        case E_USER_ERROR:
            $errtype = 'User error';
            break;
        case E_USER_WARNING:
            $errtype = 'User warning';
            break;
        case E_USER_NOTICE:
            $errtype = 'User notice';
            break;
        case E_RECOVERABLE_ERROR:
            $errtype = 'Recoverable error';
            break;
        case E_ALL:
            $errtype = 'All';
            break;
        default:
            $errtype = 'Unknown error';;
    }

    echo "发生了『{$errtype}』错误<br>" . PHP_EOL;
    echo "错误原因:{$errstr}<br>" . PHP_EOL;
    echo "错误文件:{$errfile}<br>" . PHP_EOL;
    echo "错误行号:{$errline}<br>" . PHP_EOL;

    // return FALSE; // 如果函数返回FALSE则会继续执行标准错误处理程序
}

set_error_handler('my_error'); // 将错误交给自定义函数my_error()处理

echo $test; // 由于{$test}变量未定义,echo该变量会报一个Notice错误
/********** 输出结果·开始 **********
发生了『Notice』错误
错误原因:Undefined variable: test
错误文件:D:\hosting\wwwroot\www_manong_life\htdocs\test.php
错误行号:51
********** 输出结果·结束 **********/

Copyright © 2023 码农人生. All Rights Reserved