繁体   English   中英

PHP 5禁用严格的标准错误

[英]PHP 5 disable strict standards error

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

有人可以帮忙吗?

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

# 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 只是为了隐藏错误。 [在文件中放入以下一行]

php_flag display_errors off

要么

php_value display_errors 0

接下来,设置错误报告

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代码中将E_ALL设置为error_reporting标志的值(可能在web-root中的前端控制器如index.php中,如下所示:

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

在php.ini中设置:

error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT

WordPress的

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

我没有看到一个干净且适合生产就绪软件的答案,所以在这里:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM