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

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

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

还有什么要做?


当前回答

在php.ini中:

display_errors = on

然后重新启动web服务器。

其他回答

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

并在加载文件夹时运行。

您无法在运行时启用错误输出的同一文件中捕获解析错误,因为它会在实际执行任何操作之前解析文件(因为在执行过程中遇到错误,所以不会执行任何操作)。您需要更改实际的服务器配置,以便启用display_errors并使用适当的error_reporting级别。如果您没有访问php.ini的权限,则可以使用.htaccess或类似文件,具体取决于服务器。

此问题可能提供其他信息。

我通常会在我的普通PHP项目中使用以下代码。

if(!defined('ENVIRONMENT')){
    define('ENVIRONMENT', 'DEVELOPMENT');
}

$base_url = null;

if (defined('ENVIRONMENT'))
{
    switch (ENVIRONMENT)
    {
        case 'DEVELOPMENT':
            $base_url = 'http://localhost/product/';
            ini_set('display_errors', 1);
            ini_set('display_startup_errors', 1);
            error_reporting(E_ALL);
            break;

        case 'PRODUCTION':
            $base_url = 'Production URL'; /* https://google.com */
            error_reporting(E_ALL);
            ini_set('display_errors', 0);
            ini_set('display_startup_errors', 0);
            ini_set('log_errors', 1); // Mechanism to log errors
            break;

        default:
            exit('The application environment is not set correctly.');
    }
}

如果尽管遵循了以上所有答案(或者您无法编辑php.ini文件),但仍然无法收到错误消息,请尝试创建一个新的php文件来启用错误报告,然后将问题文件包括在内。如:

error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('problem_file.php');

尽管在php.ini文件中正确设置了所有内容,但这是我唯一能够捕获名称空间错误的方法。我的确切设想是:

//file1.php
namespace a\b;
class x {
    ...
}

//file2.php
namespace c\d;
use c\d\x; //Dies because it's not sure which 'x' class to use
class x {
    ...
}

在Unix CLI中,只将错误重定向到文件是非常实用的:

./script 2> errors.log

在脚本中,可以像往常一样使用var_dump()或等效方法(STDOUT和STDERR都将接收输出),但只能在日志文件中写入:

fwrite(STDERR, "Debug infos\n"); // Write in errors.log^

然后从另一个shell中进行实时更改:

tail -f errors.log

或者干脆

watch cat errors.log