PHP错误实例详解

错误级别

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

配置项

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之外的所有错误

设置错误级别

可通过以下三种方法修改

  • 修改php.inierror_reporting, 需要重启php

  • 在程序中设置error_reporting()函数

  • 在程序中使用ini_set()函数设置

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

结果如下:

就到这里吧,一切都结束了
我觉得我还可以再抢救下
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • //禁用错误报告 error_reporting(0); //报告运行时错误 error_reporting(E_...
    沁心轩墨阅读 617评论 1 1
  • php.ini设置,上传大文件: post_max_size = 128Mupload_max_filesize ...
    bycall阅读 6,822评论 3 64
  • 文件加载 语法形式:include、include_once、require、require_once4 种语法都...
    THEyAnJ阅读 569评论 0 1
  • 参考资料:PHP5 权威编程 变量 定义:$变量名 = 值; 例如:$demo = 1; 变量的基...
    _1633_阅读 1,693评论 1 4
  • 轮回中你辗转了几亿年, 你的影子在我脑海里浮现了又几遍, 我把你的名字写进了诗篇, 你成为了我心中仅剩下的唯一留念...
    墨疏璃阅读 284评论 1 2