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

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

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

还有什么要做?


当前回答

上面的代码应该可以工作:

error_reporting(E_ALL);

但是,请尝试在文件中编辑手机上的代码:

error_reporting =on

其他回答

在php.ini中:

display_errors = on

然后重新启动web服务器。

如果是快速调试,您可以使用的最佳/简单/快速解决方案是用捕获异常包围代码。当我想在生产中快速检查一些东西时,这就是我正在做的。

try {
    // Page code
}
catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

上面的代码应该可以工作:

error_reporting(E_ALL);

但是,请尝试在文件中编辑手机上的代码:

error_reporting =on

为了显示解析错误,可以使用技巧:使用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)

由于不清楚您使用的是什么操作系统,这些是我的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" 
      } 
    } 
  ] 

并在加载文件夹时运行。