PHP5禁用严格标准错误

PHP5禁用严格标准错误,php,strict,Php,Strict,我需要在顶部设置PHP脚本,以禁用严格标准的错误报告 有人能帮忙吗?请不要出错 错误报告(0) 或者只是不严格 错误报告(E_ALL^E_STRICT) 如果您想再次显示所有错误,请使用 错误报告(-1)您想禁用错误报告,还是仅仅阻止用户看到它?记录错误通常是个好主意,即使是在生产站点上 # in your PHP code: ini_set('display_errors', '0'); # don't show any errors... error_reporting(E_ALL

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

有人能帮忙吗?

请不要出错

错误报告(0)

或者只是不严格

错误报告(E_ALL^E_STRICT)

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


错误报告(-1)

您想禁用错误报告,还是仅仅阻止用户看到它?记录错误通常是个好主意,即使是在生产站点上

# 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\u log
指令指定错误的确切位置。

如果未在php.ini中设置,则error\u reporting标志的默认值为E\u ALL&~E\u NOTICE。 但在某些安装(特别是针对开发环境的安装)中,已将E|u ALL | E|u STRICT设置为此标志的值(这是开发期间的建议值)。在某些情况下,特别是当您想要运行一些在PHP5.3时代之前开发的、尚未使用PHP5.3定义的最佳实践进行更新的开源项目时,在您的开发环境中,您可能会遇到一些类似于您所得到的消息。应对这种情况的最佳方法是,在php.inicode中,仅将E\u ALL设置为error\u reporting标志的值(可能在web root中的index.php这样的前端控制器中,如下所示:

if(defined('E_STRICT')){
    error_reporting(E_ALL);
}

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

php_flag display_errors off

接下来,设置错误报告

php_value error_reporting 30719
如果您想知道值
30719
是如何来的,那么E_ALL(32767)、E_STRICT(2048)实际上是常量,用于保存数值和php.ini集合中的(
32767-2048=30719

error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT

WordPress


如果您在wordpress环境中工作,wordpress会在函数
wp\u debug\u mode()
中的wp includes/load.php文件中设置错误级别。因此,您必须在调用此函数后更改错误级别(在未签入git的文件中,仅用于开发),或者直接修改
错误报告()
call

我没有看到一个干净的、适合于生产就绪软件的答案,因此它是这样的:

/*
 * 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);


@451F:我认为这里的关键词是“严格标准”。我不知道以前的版本,但在PHP 5.4.0中,建议您将错误报告设置为
E_ALL&~E_DEPRECATED&~E_STRICT
。请注意,他们建议您禁用严格的标准。另外,请找到PHP.ini文件并将其复制到/usr/local/php5/lib/+1:我相信
^
只适用于忽略错误类型。如果要关闭其他类型,应使用
e_ALL&~e_DEPRECATED&~e_STRICT
格式。或者可以使用
(e_ALL&~(e_DEPRECATED | e_STRICT))
format。注意:E_STRICT自PHP5以来仅是E_ALL的一部分。4@FakeCodeMonkeyRashid我想知道这是为什么?可能是因为求值顺序很重要?禁止在PHP<5.4
ini\u集中报告严格错误('error\u reporting',E\u ALL&~E\u STRICT);
禁止在PHP>=5.4
ini\u集中报告严格错误('error_reporting',E_ALL^E_STRICT)
我想指出,使用
^
(“xor”)而不是
&
(“and not”)是一个坏主意!
^
取决于这样一个假设,即E_STRICT是E_ALL的一部分,并且永远是E_ALL的一部分。这是不好的,因为E_ALL在过去确实发生了变化(E_STRICT没有过时,但现在是从PHP5.4开始的)。如果有一天假设失败,
^
不仅会打破,而且实际上会做与它应该做的相反的事情:它将启用E_STRICT,因为XOR(
^
)工作。
&
但是,无论当前的E_ALL值是多少,都将始终禁用E_STRICT。因此,应该使用
&
。非常感谢-这在PHP5.4.7中实现了这个技巧(.htaccess解决方案)-没有其他任何东西-甚至修改.ini-可以实现这个技巧。我使用了
PHP_admin_value error_reporting
(在vhost配置中).@Seza,更正并修复了它。这与页面无关,最好使用此方法,因为大多数E_-STRICT错误都是在编译时发生的,不能在运行时覆盖。Hi只是为了简化,对于使用wamp的用户,可以通过单击php>php设置>>显示错误来禁用错误。如果选中,则取消选中它。只是为了明确说明显而易见:当然,您也可以在
php.ini
文件中设置这些,例如,如果您无法修改php代码。但是,在生产中记录严格的错误也是一种不好的做法。因为您会在日志中填入可能无关紧要的通知,从而导致以下一个或两个问题:serverAdmin将错过/忽略错误和日志目录ectory将在某个时候消耗所有服务器空间。这对我不起作用-必须使用下面答案中的伪代码Monkey Rashid注释中的E_all&~E_STRICT('display_errors','0')与我在php.ini日志中找到的以下内容一起工作吗;最后一组是否成功?@nate当你发布一些代码时,请告诉我们可以将其粘贴到哪里。我不知道将其放置在哪里:放入
php.ini
.htaccess
或我的php代码中的某个地方。这非常有用,我为一个Wordpress安装打开了调试模式,但没有意识到它是这样做的。感谢提供信息!
/*
 * 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);