PHP 5禁用严格标准错误

PHP 5 disable strict standards error

我需要在顶部设置PHP脚本以禁用严格标准的错误报告。

有人可以帮忙吗?


您要禁用错误报告,还是只是阻止用户查看错误报告?即使在生产站点上,记录错误也通常是个好主意。

1
2
3
# in your PHP code:
ini_set('display_errors', '0');     # don't show any errors...
error_reporting(E_ALL | E_STRICT);  # ...but do log them

它们将被记录到您的标准系统日志中,或使用error_log指令来指定您希望错误出现的确切位置。


没有错误。

error_reporting(0);

还是不严格

error_reporting(E_ALL ^ E_STRICT);

如果您想再次显示所有错误,请使用

error_reporting(-1);


以上所有解决方案都是正确的。但是,当我们谈论普通的PHP应用程序时,必须将其包含在所需的每个页面中。解决此问题的一种方法是通过根文件夹中的.htaccess
只是为了掩盖错误。 [将以下行之一放入文件中]

1
php_flag display_errors off

要么

1
php_value display_errors 0

接下来,设置错误报告

1
php_value error_reporting 30719

如果您想知道30719的值是怎么来的,则E_ALL(32767),E_STRICT(2048)实际上是常量,它们保存数值和(32767 - 2048 = 30719)


如果未在php.ini中设置,则error_reporting标志的默认值为E_ALL和?E_NOTICE。
但是在某些安装中(尤其是针对开发环境的安装)具有E_ALL | E_STRICT设置为该标志的值(这是开发期间的建议值)。在某些情况下,特别是当您要运行一些开源项目时,这些项目是在PHP 5.3时代之前开发的,并且尚未使用PHP 5.3定义的最佳实践进行更新,因此在开发环境中,您可能会遇到一些麻烦。您收到的消息。应对这种情况的最佳方法是在php.ini或代码中(可能在web根目录中的index.php之类的前控制器中)仅将E_ALL设置为error_reporting标志的值。

1
2
3
if(defined('E_STRICT')){
    error_reporting(E_ALL);
}

在php.ini中设置:

1
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT

WordPress的

如果您在Wordpress环境中工作,Wordpress会在函数wp_debug_mode()中的wp-includes / load.php文件中设置错误级别。因此,您必须在调用此函数后更改级别(在未检入git的文件中,仅用于开发),或者直接修改error_reporting()调用


我没有一个干净的答案,不适合生产就绪的软件,所以就这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
 * Get current error_reporting value,
 * so that we don't lose preferences set in php.ini and .htaccess
 * and accidently reenable message types disabled in those.
 *
 * If you want to disable e.g. E_STRICT on a global level,
 * use php.ini (or .htaccess for folder-level)
 */

$old_error_reporting = error_reporting();

/*
 * Disable E_STRICT on top of current error_reporting.
 *
 * Note: do NOT use ^ for disabling error message types,
 * as ^ will re-ENABLE the message type if it happens to be disabled already!
 */

error_reporting($old_error_reporting & ~E_STRICT);


// code that should not emit E_STRICT messages goes here


/*
 * Optional, depending on if/what code comes after.
 * Restore old settings.
 */

error_reporting($old_error_reporting);