我检查了我的PHP ini文件(PHP.ini),并设置了display_errors,错误报告也是E_ALL。我已重新启动Apache Web服务器。

我甚至把这些行放在了脚本的顶部,它甚至不能捕捉到简单的解析错误。例如,我用“$”声明变量,而不关闭语句“;”。但我的所有脚本都显示了这些错误的空白页面,但我希望在浏览器输出中看到这些错误。

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

还有什么要做?


当前回答

为了显示解析错误,可以使用技巧:使用include,而不是在php.ini中设置display_errors。

以下是三段代码:

文件:tst1.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
// Missing " and ;
echo "Testing

当直接运行此文件时,如果php.ini中将display_errors设置为0,它将不显示任何内容。

现在,试试这个:

文件:tst2.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
include ("tst3.php");

文件:tst3.php

<?php
// Missing " and ;
echo "Testing

现在运行设置错误报告的tst2.php,然后包含tst3。您将看到:

分析错误:语法错误,意外的文件结尾,第4行tst3.php中需要变量(T_variable)或${(T_DOLLAR_OPEN_CURLY_BRACES)或{$(T_CURLY_OPEN)

其他回答

只需写下:

error_reporting(-1);

Use:

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

这是编写它的最佳方法,但语法错误会产生空白输出,因此请使用控制台检查语法错误。调试PHP代码的最佳方法是使用控制台;运行以下操作:

php -l phpfilename.php

报告除E_NOTICE之外的所有错误

error_reporting(E_ALL & ~E_NOTICE);

显示所有PHP错误

error_reporting(E_ALL);  or ini_set('error_reporting', E_ALL);

关闭所有错误报告

error_reporting(0);

这对我来说总是有效的:

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

然而,这并不能使PHP显示同一文件中发生的解析错误-显示这些错误的唯一方法是使用以下行修改PHP.ini:

display_errors = on

(如果您没有访问php.ini的权限,那么在.htaccess中输入这一行也可以):

php_flag display_errors 1

请注意,上述建议仅适用于开发环境。在实时站点上,display_errors必须设置为0,而log_errors设置为1。然后,您将能够在错误日志中看到所有错误。

在AJAX调用的情况下,在开发服务器上打开DevTools(F12),然后打开Network选项卡。然后启动您希望看到的结果的请求,它将显示在“网络”选项卡中。单击它,然后单击“响应”选项卡。在那里您将看到确切的输出。在实时服务器上,只需检查错误日志即可。

如果您发现自己处于无法通过php.ini或.htaccess修改设置的情况下,那么当您的php脚本包含解析错误时,您就不可能显示错误。然后,您必须解决以下问题:

find . -name '*.php' -type f -print0 | xargs -0 -n1 -P8 php -l | grep -v "No syntax errors"

如果您的主机被锁定,不允许通过php.ini或.htaccess更改值,那么它也可能不允许通过ini_set更改值。您可以使用以下PHP脚本进行检查:

<?php
if( !ini_set( 'display_errors', 1 ) ) {
  echo "display_errors cannot be set.";
} else {
  echo "changing display_errors via script is possible.";
}