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:简书阅读原文】。感谢您的支持!

您可能感兴趣的

PHP User List entry preg_match I am working on a small modification to a WordPress plugin. The user enters in the page/post numbers into a textbox that...
8 Awesome PHP Web Scraping Libraries and Tools Web scraping is something developers encounter as a task on a daily basis. There could be different needs as fa...
A data entry form with sanity checks I’m currently working on a large longitudinal project as a programmer/analyst. Most of the data are collected using pape...
Jquery and php, load images and use the ... I have a page that display a group of images with the src of each image, but I need to use the src valu of the images. T...
使用PHP从扩展公钥生成比特币钱包地址... 重复使用相同的比特币钱包地址是一个很大的隐私问题。如果你有一个简单的电子商店或要求捐赠的网站,你可能需要考虑为每笔交易生成唯一的地址。 有很多支付系统,如Bitpay,为你完成所有艰苦的工作。缺点是他们需要使用你的私钥。但是,你可以使用...