PHP的錯誤報錯級別設定原理簡析

2022-02-01 04:33:38 字數 2279 閱讀 5232

摘錄php.ini檔案的預設配置(php5.4):

; common values:

; e_all (show all errors, warnings and notices including coding standards.)

; e_all & ~e_notice (show all errors, except for notices)

; e_all & ~e_notice & ~e_strict (show all errors, except for notices and coding standards warnings.)

; e_compile_error|e_recoverable_error|e_error|e_core_error (show only errors)

; default value: e_all & ~e_notice & ~e_strict & ~e_deprecated

; development value: e_all

; production value: e_all & ~e_deprecated & ~e_strict

; error_reporting = e_all & ~e_notice

預設是e_all & ~e_notice。這句話的意思是show all errors, except for notices,即顯示除了notice型別的所有錯誤。為什麼運算子是& ~呢?

先看php手冊預定義常量

摘錄一部分常量:

值       常量                              說明

1 e_error ( integer ) 致命的執行時錯誤。這類錯誤一般是不可恢復的情況,例如記憶體分配導致的問題。後果是導致指令碼終止不再繼續執行。

2 e_warning ( integer ) 執行時警告 (非致命錯誤)。僅給出提示資訊,但是指令碼不會終止執行。

4 e_parse ( integer ) 編譯時語法解析錯誤。解析錯誤僅僅由分析器產生。

8 e_notice ( integer ) 執行時通知。表示指令碼遇到可能會表現為錯誤的情況,但是在可以正常執行的指令碼裡面也可能會有類似的通知。

16 e_core_error ( integer ) 在php初始化啟動過程中發生的致命錯誤。該錯誤類似 e_error ,但是是由php引擎核心產生的。

2048 e_strict ( integer ) 啟用 php 對**的修改建議,以確保**具有最佳的互操作性和向前相容性。

30719 e_all ( integer ) e_strict 出外的所有錯誤和警告資訊。

e_all:30719 in php 5.3.x, 6143 in php 5.2.x, 2047 previously

上面的值(數值或者符號)用於建立乙個二進位制位掩碼,來制定要報告的錯誤資訊。可以使用按位運算子來組合這些值或者遮蔽某些型別的錯誤。請注意,在 php.ini 之中,只有'|', '~', '!', '^' 和 '&' 會正確解析。

看下面的分析:

e_all & ~e_notice

=> 111011111111111 & ~ 1000

=> 111011111111111 & 0111

=> 111011111110111

估計看了上面大家就知道是怎麼回事了,為什麼不是e_all & e_notice

& ~e_notice剛好使第四位的值置0。如果去掉~則達不到效果。

1、可以修改php.ini進行配置,找到error_reporting

2、ini_set()error_reporting()

ini_set('display_errors', 1); 

error_reporting(e_all & ~e_notice);//排除notice錯誤

error_reporting(e_error | e_parse | e_core_error);

//只考慮致命的執行時錯誤、新解析錯誤和核心錯誤

PHP錯誤報告級別

error reporting e all e notice 錯誤報告級別是位字段的疊加,推薦使用 e all e strict 1 e error 致命的執行時錯誤 2 e warning 執行時警告 非致命性錯誤 4 e parse 編譯時解析錯誤 8 e notice 執行時提醒 經常是 bu...

PHP 設定錯誤報告

總結 php能夠在執行時動態設定是否顯示錯誤 顯示的錯誤級別。例子 不輸出錯誤報告 error reporting 0 輸出給定級別的錯誤 error reporting e error e warning e parse e notice 輸出除了e notice的他所有錯誤 error repo...

php錯誤級別的設定方法

php在執行時,針對嚴重程度不同的錯誤,會給以不同的提示。eg 在 a沒宣告時,直接相加,值為null,相加時當成0來算.但是,卻提示notice,即注意.我們在開發中,為了程式的規範性,把報錯級別,調的比較高notice級別的也報出來,有助於我們快速定位錯誤和 規範,但是,在產品上線後,運營 過程...