首頁  >  文章  >  後端開發  >  如何在PHP文件中顯示錯誤?

如何在PHP文件中顯示錯誤?

王林
王林轉載
2023-08-19 20:41:19783瀏覽

如何在PHP文件中顯示錯誤?

A PHP application produces many levels of errors during the runtime of the script . So in this article, we will learn how to display all the errors and warning messages.

The quickest way to display all php errors and warnings is to add these lines to your PHP code file:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

ini_set函數將嘗試覆蓋php.ini檔案中的設定。如果在php.ini檔案中關閉了display_error,它將在程式碼中開啟它。它還將display_startup_errors設為true以顯示錯誤訊息。 error_reporting()是一個原生的PHP函數,用來顯示錯誤。透過將其設為true,它會顯示在程式碼中發生的錯誤。

但是又出現了一個問題,什麼是E_ALL?答案很簡單,PHP程式碼會產生不同等級的錯誤。讓我們了解一下在PHP程式碼中發生的錯誤類型。

  • E_ERROR:

    致命的執行階段錯誤,​​腳本的執行已停止
  • E_WARNING:

    非致命的執行階段錯誤,​​腳本的執行已停止
  • E_PARSE:

    編譯時錯誤,由解析器產生
  • E_NOTICE:

    腳本發現可能是錯誤的東西
  • E_CORE_ERROR:

    腳本初始啟動時發生的致命錯誤
  • E_CORE_WARNING:

    在腳本初始啟動時發生的非致命錯誤
  • E_ALL:

    所有錯誤和警告
  • 不幸的是,我們寫的上述程式碼幾乎不會顯示解析錯誤,例如缺少分號或缺少花括號。在這種情況下,必須修改php.ini配置。

    display_errors = on

    在php.ini文件中,display_errors指令必須設定為"on"。這將顯示所有錯誤,包括語法或解析錯誤,這些錯誤不能透過在PHP程式碼中呼叫ini_set函數來顯示。

    因此,透過上述方式,我們可以在我們的php應用程式中顯示錯誤。
#

以上是如何在PHP文件中顯示錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除