如何在PHP文件中显示错误?

王林
王林 转载
2023-08-19 20:42:02 254浏览

如何在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,如有侵犯,请联系admin@php.cn删除