技术栈

主页 > 移动开发 >

PHP错误实例详解

技术栈 - 中国领先的IT技术门户

错误级别

php.ini中可查看PHP的各个错误级别

; Error Level Constants:
; E_ALL             - All errors and warnings (includes E_STRICT as of PHP 5.4.0)
; E_ERROR           - fatal run-time errors
; E_RECOVERABLE_ERROR  - almost fatal run-time errors
; E_WARNING         - run-time warnings (non-fatal errors)
; E_PARSE           - compile-time parse errors
; E_NOTICE          - run-time notices (these are warnings which often result
;                     from a bug in your code, but it's possible that it was
;                     intentional (e.g., using an uninitialized variable and
;                     relying on the fact it is automatically initialized to an
;                     empty string)
; E_STRICT          - run-time notices, enable to have PHP suggest changes
;                     to your code which will ensure the best interoperability
;                     and forward compatibility of your code
; E_CORE_ERROR      - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING    - warnings (non-fatal errors) that occur during PHP's
; E_STRICT          - run-time notices, enable to have PHP suggest changes
;                     to your code which will ensure the best interoperability
;                     and forward compatibility of your code
; E_CORE_ERROR      - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING    - warnings (non-fatal errors) that occur during PHP's
;                     initial startup
; E_COMPILE_ERROR   - fatal compile-time errors
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
; E_USER_ERROR      - user-generated error message
; E_USER_WARNING    - user-generated warning message
; E_USER_NOTICE     - user-generated notice message
; E_DEPRECATED      - warn about code that will not work in future versions
;                     of PHP
; E_USER_DEPRECATED - user-generated deprecation warnings

配置项

php.ini与错误相关的配置项:

选项描述
error_reporting设置错误报告的级别
display_errors是否显示错误
log_errors是否将错误记录到日志中
error_log设置错误将存储到的文件路径
log_errors_max_len设置log_errors的最大字节数
ingnore_repeated_errors是否忽略重复的错误
ingore_repeated_source是否忽略重复信息的来源

error_reporting = E_ALL & ~ E_NOTICE # 表示除了NOTICE之外的所有错误

设置错误级别

可通过以下三种方法修改

echo error_reporting(); // 结果是用数字值表示
echo "<br/>";
error_reporting(E_ALL);
echo $test; // 报Notice错误
echo "<br/>";
error_reporting(E_ALL & ~ E_NOTICE); // NOTICE级别错误不显示
echo $name; // 不会报错

error_reporting(0); // 不显示所有错误
settype($var, 'test'); // 不报错
error_reporting(-1); // 显示所有错误
settype($var, 'test'); // warning错误

echo "continue"; // 非致命错误,程序会继续往下跑
ini_set('error_reporting', -1);
ini_set('display_errors', 0); // 不显示错误
echo $name;
ini_set('display_errors', 1); // 显示错误
echo $age;

以上错误属于noticewarning级别,不会阻断程序,如果是Fatal级别或Parse error语法错误的错误,程序会立即中止。

md6('md5有md6厉害吗?');
echo "程序还能往下跑?"; // Fatal error,此行不会运行

手动触发PHP错误

除了PHP解释器能触发错误外,开发人员也可以通过trigger_error函数触发

header('content-type:text/html;charset=utf-8');
$name = '张飞';
$gender = '女';
trigger_error('扯淡,张飞是猛男', E_USER_ERROR); // 自定义的Fatal error
echo "还能运行吗?"; // 不会运行

自定义错误处理器

我们可以通过set_error_handler函数接管php的错误处理方法

header('content-type:text/html;charset=utf-8');
error_reporting(-1);
function customError($errno, $errmsg, $file, $line) {
    echo "错误代码:[{$errno}] {$errmsg}<br>";
    echo "错误行号:{$file} 文件中的第{$line}行<br>";
    echo "PHP版本" . PHP_VERSION . "(" . PHP_OS . ")<br>";
}
set_error_handler('customError');

// NOTICE 和 Warning级别被有自定义的错误处理器接管
echo $test;
settype($var, 'test');

md6('又来加密了'); // Fatal error不会被自定义的错误处理器接管

脚本即将关闭前执行的函数

有时,我们需要在php脚本关闭时进行一些操作,此时可以用register_shutdown_function(),该函数能让php程序在意外终止时垂死挣扎完成最后的使命。

class Shutdown {
    public static function doSomething()
    {
        echo "我觉得我还可以再抢救下";
    }
}

echo "就到这里吧,一切都结束了<br />";
register_shutdown_function(['Shutdown', 'doSomething']);

结果如下:

就到这里吧,一切都结束了
我觉得我还可以再抢救下
责任编辑:admin  二维码分享:
本文标签: errorserrorechoPHP错误fatal