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

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

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

还有什么要做?


当前回答

如果您发现自己处于无法通过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.";
}

其他回答

这对我来说总是有效的:

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选项卡。然后启动您希望看到的结果的请求,它将显示在“网络”选项卡中。单击它,然后单击“响应”选项卡。在那里您将看到确切的输出。在实时服务器上,只需检查错误日志即可。

要显示所有错误,您需要:

1.在从浏览器(通常是index.PHP)调用的PHP脚本中包含以下行:

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

2.(a)确保此脚本没有语法错误

—or—

2.(b)在php.ini中设置display_errors=打开

否则,它甚至无法运行这两条线!

您可以通过运行(在命令行)检查脚本中的语法错误:

php -l index.php

如果包含另一个PHP脚本中的脚本,那么它将在包含的脚本中显示语法错误。例如:

索引php

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

// Any syntax errors here will result in a blank screen in the browser

include 'my_script.php';

my_script.php

adjfkj // This syntax error will be displayed in the browser

如果您正在执行SharedHosting计划(如hostgator)。。。简单地添加

php_flag display_errors 1

插入.htaccess文件并将其上载到远程文件夹可能不会产生服务器上生成的实际警告/错误。

您还需要编辑php.ini

这是通过cPanel(在hostgator共享主机上测试)实现的计划)

登录cPanel后,搜索MultiPHP INI编辑器。它通常位于cPanel项目列表中的“软件”部分。

在MultiPHP INI编辑器页面上。。。您可以停留在基本模式选项卡上,只需选中行中显示displayerrors的按钮。然后单击应用按钮保存。

重要事项:只需记住在完成调试时将其关闭;因为这不建议用于公共服务器。

警告:以下答案事实上不正确。错误处理中没有任何更改,未捕获的异常与其他错误一样显示。建议的方法必须谨慎使用,因为它无条件地输出错误,尽管设置了display_error,并且可能会通过向现场的局外人透露敏感信息而构成威胁。

您可能会发现“错误报告”或“显示错误”的所有设置在PHP7中似乎都不起作用。这是因为错误处理已更改。请尝试以下操作:

try{
     // Your code
} 
catch(Error $e) {
    $trace = $e->getTrace();
    echo $e->getMessage().' in '.$e->getFile().' on line '.$e->getLine().' called from '.$trace[0]['file'].' on line '.$trace[0]['line'];
}

或者,要一次性捕获异常和错误(这与PHP5不向后兼容):

try{
     // Your code
} 
catch(Throwable $e) {
    $trace = $e->getTrace();
    echo $e->getMessage().' in '.$e->getFile().' on line '.$e->getLine().' called from '.$trace[0]['file'].' on line '.$trace[0]['line'];
}

由于不清楚您使用的是什么操作系统,这些是我的2美分Windows。如果您正在使用XAMPP,则需要在C:\xamp.php下手动创建日志文件夹。不是你的错,ApacheFriends提交了这个。

要阅读并遵循此文件,请执行以下操作。

Get-Content c:\xampp\php\logs\php_error_log -Wait

要在VSCode中执行此操作,请在.VSCode\tasks.json中创建一个任务

{ 
  // See https://go.microsoft.com/fwlink/?LinkId=733558 
  // for the documentation about the tasks.json format 
  "version": "2.0.0", 
  "tasks": [ 
    { 
      "label": "Monitor php errors", 
      "type": "shell", 
      "command": "Get-Content -Wait c:\\xampp\\php\\logs\\php_error_log", 
      "runOptions": { 
        "runOn": "folderOpen" 
      } 
    } 
  ] 

并在加载文件夹时运行。