PHP 5 禁用严格标准错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1248952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:43:38  来源:igfitidea点击:

PHP 5 disable strict standards error

phpstrict

提问by Manny Calavera

I need to setup my PHP script at the top to disable error reporting for strict standards.

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

Can anybody help ?

有人可以帮忙吗?

回答by Nate

Do you want to disable error reporting, or just prevent the user from seeing it? It's usually a good idea to log errors, even on a production site.

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

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

They will be logged to your standard system log, or use the error_logdirective to specify exactly where you want errors to go.

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

回答by Tyler Carter

For no errors.

因为没有错误。

error_reporting(0);

error_reporting(0);

or for just not strict

或者只是不严格

error_reporting(E_ALL ^ E_STRICT);

error_reporting(E_ALL ^ E_STRICT);

and if you ever want to display all errors again, use

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

error_reporting(-1);

error_reporting(-1);

回答by Starx

All above solutions are correct. But, when we are talking about a normal PHP application, they have to included in every page, that it requires. A way to solve this, is through .htaccessat root folder. Just to hide the errors. [Put one of the followling lines in the file]

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

php_flag display_errors off

Or

或者

php_value display_errors 0

Next, to set the error reporting

接下来,设置错误报告

php_value error_reporting 30719

If you are wondering how the value 30719came, E_ALL (32767), E_STRICT (2048) are actually constant that hold numeric value and (32767 - 2048 = 30719)

如果你想知道这个值30719是怎么来的,E_ALL (32767), E_STRICT (2048) 实际上是保存数值和 ( 32767 - 2048 = 30719)

回答by M N Islam Shihan

The default value of error_reportingflag is E_ALL & ~E_NOTICEif not set in php.ini. But in some installation (particularly installations targeting development environments) has E_ALL | E_STRICTset as value of this flag(this is the recommended value during development). In some cases, specially when you'll want to run some open source projects, that was developed prior to PHP 5.3 era and not yet updated with best practices defined by PHP 5.3, in your development environment, you'll probably run into getting some messages like you are getting. The best way to cope up on this situation, is to set only E_ALLas the value of error_reportingflag, either in php.inior in code(probably in a front-controller like index.php in web-root as follows:

如果在 php.ini 中没有设置,error_reporting标志的默认值为E_ALL & ~E_NOTICE。但是在某些安装中(特别是针对开发环境的安装)有E_ALL | E_STRICT设置为此标志的值(这是开发期间推荐值)。在某些情况下,特别是当您想要运行一些开源项目时,这些项目是在 PHP 5.3 时代之前开发的,并且尚未使用 PHP 5.3 定义的最佳实践进行更新,在您的开发环境中,您可能会遇到一些您收到的消息。应对这种情况的最佳方法是仅将E_ALL设置为error_reporting标志的值,无论是在php.ini代码(可能在前端控制器中,如 web-root 中的 index.php 如下:

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

回答by MSS

In php.ini set :

在 php.ini 中设置:

error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT

回答by Nicola Peluchetti

WordPress

WordPress

If you work in the wordpress environment, Wordpress sets the error level in file wp-includes/load.php in function wp_debug_mode(). So you have to change the level AFTER this function has been called ( in a file not checked into git so that's development only ), or either modify directly the error_reporting()call

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

回答by Jay

I didn't see an answer that's clean and suitable for production-ready software, so here it goes:

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

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