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

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

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

还有什么要做?


当前回答

这将起作用:

<?php
     error_reporting(E_ALL);
     ini_set('display_errors', 1);    
?>

其他回答

我通常会在我的普通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.');
    }
}

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

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

如果安装了Xdebug,则可以通过以下设置覆盖每个设置:

xdebug.force_display_errors = 1;
xdebug.force_error_reporting = -1;

强制显示错误类型:int,默认值:0,在Xdebug中引入>=2.3如果设置设置为1,则无论PHP的display_errors的设置是什么。强制错误报告类型:int,默认值:0,在Xdebug中引入>=2.3此设置是位掩码,如error_reporting。此位掩码将与error_reporting表示的位掩码进行逻辑“或”运算,以将错误显示到dermine。此设置只能在php.ini中进行,允许您强制显示某些错误,无论应用程序如何使用ini_set()。

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

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

这将起作用:

<?php
     error_reporting(E_ALL);
     ini_set('display_errors', 1);    
?>