如何显示 PHP 错误?

Posted

技术标签:

【中文标题】如何显示 PHP 错误?【英文标题】:How do I get PHP errors to display? 【发布时间】:2020-10-12 01:22:50 【问题描述】:

我检查了我的 php ini 文件 (php.ini) 并设置了 display_errors 并且错误报告是 E_ALL。我已经重新启动了我的 Apache 网络服务器。

我什至将这些行放在脚本的顶部,它甚至没有捕获简单的解析错误。例如,我用"$" 声明变量并且我不关闭语句";"。但是我所有的脚本都在这些错误上显示了一个空白页面,但我希望在浏览器输出中真正看到 errors

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

还有什么事情要做?

【问题讨论】:

我还没有确切地确定为什么这有时会起作用,但对于任何想要在 php 脚本中快速切换错误(或通过 $_REQUEST 参数启用它们)的人来说,这两行大部分时间都可以工作。 那么你可以通过从 php ini 文件中启用 xdebug 来查看错误的详细信息。 大多数特定的编辑器/IDE,例如Notepad++,Eclipse 有内置的语法检查和突出显示,它们会向您显示您所描述的问题。请不要在实时系统上打开错误显示。黑客会喜欢这个,因为在大多数情况下都会显示路径。您可以定义错误/异常处理程序。在这个处理程序中,您可以记录问题并向开发人员发送邮件,以便他可以在问题发生时立即修复它。见php.net/manual/en/function.set-error-handler.php和php.net/manual/en/function.set-exception-handler.php 【参考方案1】:

这对我总是有效的:

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

【讨论】:

另外注意可以使用这3行,然后include('fileImWorkingOn.php');。然后你也可以捕捉到语法错误! 虽然我不是 SysOps,但我认为拥有 .htaccess 文件的人比拥有 php.ini 文件的人多,而且这些都会在解析之前出现,对吧? php_flag display_errors 1 为 .htaccess E_ALL 不足以显示 PHP 5.3 中的 all 错误。 “E_STRICT 在 5.4.0 中成为 E_ALL 的一部分” - PHP Manual 在该版本中您需要 E_ALL | E_STRICT-1 哥们,我要纹这三行 @JuanC.Roldán 我实际上每周都会回到这个答案三次。【参考方案2】:

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

This question 可能会提供更多信息。

【讨论】:

【参考方案3】:

在你的 php.ini 中:

display_errors = on

然后重新启动您的网络服务器。

【讨论】:

+①。在我的 ubuntu /etc/php5/apache2/php.ini 用于重启(Debian、Ubuntu等)sudo service apache2 restart 在 OS X 上重启 sudo apachectl -k restart. 有趣的事实:如果你简单地输入 phpinfo(); 就可以找到你加载的 php.ini 文件。进入一个空白的php文件。这是第 7 行,名为 Loaded Configuration File 这对我不起作用。事实上,我可以调用ini_get('display_errors'),它会返回一个空字符串(意味着它已关闭)。是的,我检查以确保它是配置文件中的唯一行。该设置以某种方式被覆盖,我不知道为什么,这让我发疯。是的,我已经搜索了 /etc/php.d/ 中的所有内容,它也不是这些文件之一。是的,我也重新启动了网络服务器。不,.htaccess 文件中没有任何内容。我正在使用 PHP 7.4.6。【参考方案4】:

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

1.在您从浏览器调用的 PHP 脚本中包含这些行(通常为 index.php):

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

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

——或——

2.(b) 在您的php.ini 中设置display_errors = On

否则,它甚至无法运行那 2 行!

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

php -l index.php

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

index.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

【讨论】:

我在执行 php -l phpfilename.php 时得到的只是一条消息,上面写着“解析 sql_lab.php 时出错”。它没有告诉我错误是什么。 除了空白屏幕外,它还会返回错误 500,这比没有内容更安全。【参考方案5】:

一些网络托管服务提供商允许您更改 .htaccess 文件中的 PHP 参数。

您可以添加以下行:

php_value display_errors 1

我遇到了和你一样的问题,这个解决方案解决了它。

【讨论】:

如果您在 nginx 环境中,则在 location ~\.php 指令下将 php 值添加到您的站点(站点可用)配置中。 fastcgi_param PHP_VALUE "error_reporting=E_ALL;\n display_errors=1;";【参考方案6】:

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

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'];

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

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'];

【讨论】:

你的意思是 PHP7 还是 PHP7.1 ?我很困惑,我尝试了经过验证的答案并且它有效,我认为您提出了一些不同的恕我直言,确实“没有向后兼容性”,如果您必须修改完整的 PHP @FancyJohn,这可能会有所帮助:$bt = debug_backtrace(); print_r($bt); @ballangddang,我遇到了 PHP 7.0 的问题,我可以显示错误的唯一方法是使用 try/catch 块并专门捕获 Error。如果您将所有请求(可能除了 javascript、CSS、图像等)重写到 index.php 文件,然后在其中放置 try catch 块,它会更容易。是的,任何没有单一入口点的系统都将是一个令人头疼的更新。 PHP 不显示未处理的异常吗?很确定吗? 它应该显示未处理的异常。如果您在开始时打开输出缓冲区(以便您可以在最终刷新响应正文之前随时发送标头),异常消息可能会在某处丢失。【参考方案7】:

这将起作用:

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

【讨论】:

2020 年 10 月:这对我有用。太简单。不要乱用 .htaccess 或 php.ini。完成后,只需将其注释掉或删除即可。 PHP 7.4【参考方案8】:

用途:

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

这是最好的写法,但是语法错误会给出空白输出,所以使用控制台检查语法错误。调试 PHP 代码的最佳方式是使用控制台;运行以下命令:

php -l phpfilename.php

【讨论】:

【参考方案9】:

在你的 index.php 文件中设置:

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

【讨论】:

【参考方案10】:

在 PHP 文件所在的文件夹中创建一个名为 php.ini 的文件。

在 php.ini 中添加以下代码(我给出了一个显示代码的简单错误):

display_errors = on

display_startup_errors = on

【讨论】:

【参考方案11】:

由于我们现在正在运行 PHP 7,因此这里给出的答案不再正确。唯一还可以的就是the one from Frank Forte,因为他谈到了 PHP 7。

另一方面,与其尝试使用 try/catch 捕获错误,不如使用一个技巧:使用 include。

这里是三段代码:

文件:tst1.php

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 'On');
    // Missing " and ;
    echo "Testing
?>

在 PHP 7 中运行它不会显示任何内容。

现在,试试这个:

文件:tst2.php

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 'On');
    include ("tst3.php");
?>

文件:tst3.php

<?php
    // Missing " and ;
    echo "Testing
?>

现在运行设置错误报告的 tst2,然后包含 tst3。你会看到:

解析错误:语法错误,文件意外结束,预期变量 (T_VARIABLE) 或 $ (T_DOLLAR_OPEN_CURLY_BRACES) 或 $ (T_CURLY_OPEN) 位于第 4 行的 tst3.php 中

【讨论】:

我花了一些时间来解决这个问题。他们为什么改变 php 5 的行为?【参考方案12】:

我通常会在我的普通 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|E_STRICT);
            break;

        case 'PRODUCTION':
            $base_url = 'Production URL'; /* https://google.com */
            error_reporting(0);
            /* Mechanism to log errors */
            break;

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

【讨论】:

【参考方案13】:

如果尽管遵循了上述所有答案(或者您无法编辑 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 
    ...

【讨论】:

不,报错不是loglevel,是bitfield。使用 999999 是一个非常糟糕的主意,请使用一些 2 的幂负 1,例如 2047! 你说得对,@peterh!我已将其更改为 E_ALL,因为这将启用报告所有错误(php 5.4 及以下版本中的严格错误除外)。【参考方案14】:

如果您发现自己无法通过php.ini.htaccess 修改设置,那么当您的PHP 脚本包含解析错误时,您将无法显示错误。然后你必须像这样解析到linting the files on the command line:

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.";

【讨论】:

find . -name '*.php' -type f -exec php -l \; | grep -v 'No syntax errors detected' 更简单【参考方案15】:

您可以执行以下操作:

在主索引文件中设置以下参数:

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

然后根据您的要求,您可以选择要显示的内容:

对于所有错误、警告和通知:

    error_reporting(E_ALL); OR error_reporting(-1);

对于所有个错误:

    error_reporting(E_ERROR);

对于所有警告:

    error_reporting(E_WARNING);

对于所有通知:

    error_reporting(E_NOTICE);

更多信息,请查看here。

【讨论】:

什么是“主索引文件”?文件index.html?【参考方案16】:

您可以添加自己的自定义错误处理程序,它可以提供额外的调试信息。此外,您可以将其设置为通过电子邮件向您发送信息。

function ERR_HANDLER($errno, $errstr, $errfile, $errline)
    $msg = "<b>Something bad happened.</b> [$errno] $errstr <br><br>
    <b>File:</b> $errfile <br>
    <b>Line:</b> $errline <br>
    <pre>".json_encode(debug_backtrace(), JSON_PRETTY_PRINT)."</pre> <br>";

    echo $msg;

    return false;


function EXC_HANDLER($exception)
    ERR_HANDLER(0, $exception->getMessage(), $exception->getFile(), $exception->getLine());


function shutDownFunction() 
    $error = error_get_last();
    if ($error["type"] == 1) 
        ERR_HANDLER($error["type"], $error["message"], $error["file"], $error["line"]);
    


set_error_handler ("ERR_HANDLER", E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
register_shutdown_function("shutdownFunction");
set_exception_handler("EXC_HANDLER");

【讨论】:

【参考方案17】:

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

error_reporting(E_ALL);

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

error_reporting =on

【讨论】:

【参考方案18】:

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

try 
    // Page code

catch (Exception $e) 
    echo 'Caught exception: ',  $e->getMessage(), "\n";

【讨论】:

对于 php7,catch (Throwable $e) 更好...或者catch(Error $e)下面的另一个catch块【参考方案19】:
    <?php
    // Turn off error reporting
    error_reporting(0);

    // Report runtime errors
    error_reporting(E_ERROR | E_WARNING | E_PARSE);

    // Report all errors
    error_reporting(E_ALL);

    // Same as error_reporting(E_ALL);
    ini_set("error_reporting", E_ALL);

    // Report all errors except E_NOTICE
    error_reporting(E_ALL & ~E_NOTICE);
    ?>

当您的网站上线时,出于安全原因,php.ini 文件应该禁用 display_errors。但是,对于开发环境,可以启用 display_errors 进行故障排除。

【讨论】:

【参考方案20】:

只写:

error_reporting(-1);

【讨论】:

【参考方案21】:

您可以通过更改 php.ini 文件并添加以下内容来做到这一点

display_errors = on
display_startup_errors = on

或者你也可以使用下面的代码,因为这对我总是有效的

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

【讨论】:

【参考方案22】:

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

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

force_display_errors

类型:int,默认值:0,Xdebug 中引入 >= 2.3 如果这样 设置为 1 则始终显示错误,无论 PHP的display_errors的设置是什么。

force_error_reporting

类型:int,默认值:0,在Xdebug中引入>= 2.3 此设置是位掩码,如 error_reporting。该位掩码将与 error_reporting 表示的位掩码进行逻辑或运算,以确定应显示哪些错误。此设置只能在 php.ini 中进行,无论应用程序使用 ini_set() 做什么,都可以强制显示某些错误。

【讨论】:

【参考方案23】:

您可能想要使用此代码:

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

【讨论】:

【参考方案24】:

报告除 E_NOTICE 之外的所有错误

error_reporting(E_ALL & ~E_NOTICE);

显示所有 PHP 错误

error_reporting(E_ALL);  or ini_set('error_reporting', E_ALL);

关闭所有错误报告

error_reporting(0);

【讨论】:

【参考方案25】:

如果是在命令行,你可以运行php-ddisplay_errors=1来覆盖php.ini中的设置:

php -ddisplay_errors=1 script.php

【讨论】:

【参考方案26】:
     error_reporting(1);
     ini_set('display_errors', '1');
     ini_set('display_startup_errors', '1');
     error_reporting(E_ALL);

将此放在页面顶部。

【讨论】:

【参考方案27】:

您可以通过简单的方式在显示器中显示 Php 错误。 首先,只需将以下代码放入您的 php.ini 文件中。

display_errors = on;

(如果您无权访问 php.ini,那么将这一行放在 .htaccess 中也可以):

php_flag display_errors 1

或者您也可以在 index.php 文件中使用以下代码

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

【讨论】:

【参考方案28】:

在Unix CLI中,重定向only errors to a file非常实用:

./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

【讨论】:

如何回答这个关于 PHP 的问题? ./script 是一个 php CLI 脚本(hashbang #!/usr/bin/php)。您可以通过这种方式重定向文件中的 php 错误。这是unix管道。这不是 php 作为 CGI。【参考方案29】:

在代码顶部输入这个

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

然后在 php.ini 文件中插入:

display_errors = on

这必须有效。

【讨论】:

【参考方案30】:

接受的回答包括额外的选项。在我的 DEVELOPMENT apache vhost 的 PHP 文件中(.htaccess,如果你能确保它不会进入生产环境):

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

但是,这不会使 PHP 显示解析错误 - 显示这些错误的唯一方法是使用以下行修改 php.ini:

display_errors = on

(如果您无权访问php.ini,则将此行放入.htaccess 也可以):

// I've added some extra options that set E_ALL as per https://www.php.net/manual/en/errorfunc.configuration.php.
php_flag log_errors on
php_flag display_errors on
php_flag display_startup_errors on
php_value error_reporting 2147483647
php_value error_log /var/www/mywebsite.ext/logs/php.error.log

【讨论】:

以上是关于如何显示 PHP 错误?的主要内容,如果未能解决你的问题,请参考以下文章

异步提交表单时如何显示 php 错误消息?

如何显示 PHP 错误?

如何显示 PHP 错误?

iis中,php如何显示运行错误

如何在代码输出中显示 PHP 错误?

如何在 PHP 自定义 mysqli 函数中显示 SQL 错误?