如何避免或限制 PHP 错误输出


How to avoid or restrict PHP Error output

由于连接速度极慢等原因,我的 PHP Web 应用程序有时会返回此Fatal error on Line XXX。虽然这种情况很少发生,比如 1000 次中有 1 次,但我想避免在浏览器上出现这样的输出。我正在服务器端为某些功能进行错误日志记录。所以,我不想完全禁用错误报告。只是我不想向最终用户显示它。

关闭

error_reporting的各种示例。

<?php
// Turn off all error reporting
error_reporting(0);
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
?>

注意:您可以在任何不想显式错误的文件中放置或include()它。或者如果你想完全摆脱它。在 php.ini 文件中进行调整。

在 php 中查找的行.ini进行调整和关闭error_reporting。

 ini_set('display_errors', 'On'); //change to Off
        error_reporting(E_ALL); // change to 0

更多信息 ::

PHP.NET

要禁止向页面用户显示任何错误,请将

display_errors = Off

在你的PHP中.ini(无论如何,这是生产网站的推荐设置!)或

ini_set('display_errors', 0);

在你的 PHP 中。

display_errors设置仅影响网页上的输出;它不会影响可能配置的日志文件;那里的消息仍将被记录。

请参阅有关配置错误报告的 php 文档:http://php.net/manual/en/errorfunc.configuration.php

注意:据我所知,其他用户在这里提到的error_reporting设置会影响所有类型的错误报告(即报告到可能配置的日志文件的内容)。如果将error_reporting设置为 0,则也不会获得任何日志条目。如果要将某些内容记录到日志文件中但不向用户显示,则可以使用 display_errors 设置。

在该

页面的开头写

  error_reporting(0);

为了避免此类问题,您可以设置 max_execution_time=1024M 以避免数据生成速度慢,并且隐藏错误必须在 php.ini 文件上error_reporting=0。

或者简单地说:

PHP Code
ini_set('max_execution_time',1024M);
error_reporting(0);
PHP Ini Settings
max_execution_time=1024M
error_reporting=0

希望对您有所帮助。

error_reporting(0);仅在不使用set_error_handler('my_function')时才有效。如果是这种情况,您已禁止显示"my_function"中的错误消息。