我检查了我的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.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 {
    ...
}

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

在index.php文件中设置:

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

如果安装了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()。

警告:以下答案事实上不正确。错误处理中没有任何更改,未捕获的异常与其他错误一样显示。建议的方法必须谨慎使用,因为它无条件地输出错误,尽管设置了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'];
}