設定錯誤報告以及顯示它們的位置

如果它還沒有在 php.ini 中完成,則可以動態設定錯誤報告,並且應該設定為允許顯示大多數錯誤:

句法

int error_reporting ([ int $level ] )

例子

// should always be used prior to 5.4
error_reporting(E_ALL);

// -1 will show every possible error, even when new levels and constants are added 
// in future PHP versions. E_ALL does the same up to 5.4.
error_reporting(-1);

// without notices
error_reporting(E_ALL & ~E_NOTICE);

// only warnings and notices.
// for the sake of example, one shouldn't report only those
error_reporting(E_WARNING | E_NOTICE);

預設情況下,php 會記錄錯誤,通常位於與執行指令碼相同級別的 error.log 檔案中。

在開發環境中,還可以在螢幕上顯示它們:

ini_set('display_errors', 1);

然而,在生產中,應該

ini_set('display_errors', 0);

並通過使用異常或錯誤處理程式顯示友好的問題訊息。