PHP错误实例详解

综合编程 2018-05-16 阅读原文

错误级别

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 "
"; error_reporting(E_ALL); echo $test; // 报Notice错误 echo "
"; 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}
"; echo "错误行号:{$file} 文件中的第{$line}行
"; echo "PHP版本" . PHP_VERSION . "(" . PHP_OS . ")
"; } 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 "就到这里吧,一切都结束了
"; register_shutdown_function(['Shutdown', 'doSomething']);

结果如下:

就到这里吧,一切都结束了
我觉得我还可以再抢救下
简书

责编内容by:简书阅读原文】。感谢您的支持!

您可能感兴趣的

Auto increment number in the PHP foreach loop This is the beginning of a php for loop. I would like to edit it so that 'INSERT NUMBER HERE' gets replaced with an incrementing number. Ie. first loo...
Why Web Application Maintenance Should Be More Of ... Traditional software developers have been hiding a secret from us in plain sight. It’s not even a disputed fact. It’s part of their business mod...
Php time ‘off’ by 8 hours and 14 minut... I have a time / date problem. I'm trying to compare the current date and time the the date / time at the beginning of the day (midnight). The current ...
Assign the value of a global variable in php from ... I have an html file in which i have defined a global variable as query("SELECT * FROM rings"); $mainRingId = 1; ?> Now I have an Image
服务器全能环境 UPUPW ANK V1.1.2 发布 柚皮Windows服务器集成环境智控平台(以下简称UPUPW ANK),主要用于一键部署全能网站运行环境,适用服务器生产环境和本地开发环境。 UPUPW ANK控制面板集成网站运行环境所需功能,简单几步即可创建PHP,JSP,ASP/ASP.NET站点。 UPUPW ...