php 开发中推荐的 error_reporting() 设置是什么?E_STRICT 呢?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/74847/
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-24 21:23:13  来源:igfitidea点击:

What is the recommended error_reporting() setting for development? What about E_STRICT?

phperror-reporting

提问by SeanDowney

Typically I use E_ALLto see anything that PHP might say about my code to try and improve it.

通常,我E_ALL经常查看 PHP 可能对我的代码说的任何内容,以尝试改进它。

I just noticed a error constant E_STRICT, but have never used or heard about it, is this a good setting to use for development? The manual says:

我刚刚注意到一个错误常量E_STRICT,但从未使用或听说过它,这是用于开发的好设置吗?手册上说:

Run-time notices. Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.

运行时通知。启用让 PHP 建议对您的代码进行更改,这将确保您的代码具有最佳的互操作性和向前兼容性。

So I'm wondering if I'm using the best error_reportinglevel with E_ALLor would that along with E_STRICTbe the best? Or is there any other combination I've yet to learn?

所以我想知道我是否使用了最好的error_reporting级别,E_ALL还是会E_STRICT是最好的?或者还有其他组合我还没有学习吗?

回答by Jim

In PHP 5, the things covered by E_STRICTare not covered by E_ALL, so to get the most information, you need to combine them:

在 PHP 5 中, 覆盖的东西E_STRICT没有被覆盖E_ALL,所以为了获得最多的信息,你需要将它们组合起来:

 error_reporting(E_ALL | E_STRICT);

In PHP 5.4, E_STRICTwill be included in E_ALL, so you can use just E_ALL.

在 PHP 5.4 中,E_STRICT将包含在 中E_ALL,因此您可以仅使用E_ALL.

You can also use

你也可以使用

error_reporting(-1);

which will always enable allerrors. Which is more semantically correct as:

这将始终启用所有错误。哪个在语义上更正确:

error_reporting(~0);

回答by Eduardo Marinho

Use the following in php.ini:

在 php.ini 中使用以下内容:

error_reporting = E_ALL | E_STRICT

Also you should install Xdebug, it can highlight your errors in blinding bright colors and print useful detailed information.

您还应该安装Xdebug,它可以突出显示您在明亮色彩中的错误并打印有用的详细信息。

Never let any error or notice in your code, even if it's harmless.

永远不要在你的代码中留下任何错误或通知,即使它是无害的。

回答by Daniel Papasian

In my opinion, the higher you set the error reporting level in development phase, the better.

在我看来,你在开发阶段设置的错误报告级别越高越好。

In a live environment, you want a slightly (but only slightly) reduced set, but you want them logged somewhere that they can't be seen by the user (I prefer syslog).

在实时环境中,您需要稍微(但只是稍微)减少的集合,但您希望它们记录在用户无法看到的地方(我更喜欢syslog)。

http://php.net/error_reporting

http://php.net/error_reporting

E_ALL | E_STRICTfor development with PHP before 5.2.0.

E_ALL | E_STRICT用于 5.2.0 之前的 PHP 开发。

5.2 introduces E_RECOVERABLE_ERRORand 5.3 introduces E_DEPRECATEDand E_USER_DEPRECATED. You'll probably want to turn those on if you're running one of those versions.

5.2 介绍E_RECOVERABLE_ERROR和 5.3 介绍E_DEPRECATEDE_USER_DEPRECATED。如果您正在运行这些版本之一,您可能想要打开它们。

If you wanted to use magic numbers you could just set the error_reportingvalue to some fairly high value of 2^n-1- say, 16777215, and that would really just turn on all the bits between 1..n. But I don't think using magic numbers is a good idea...

如果您想使用幻数,您可以将error_reporting值设置为某个相当高的值2^n-1- 例如,16777215,这实际上只会打开 之间的所有位1..n。但我不认为使用幻数是个好主意......

In my opinion, PHP has dropped the ball a bit by having E_ALLnot really be all. But apparently it's going to be fixed in PHP 6...

在我看来,PHPE_ALL并不是真正的全部。但显然它将在 PHP 6 中修复......

回答by RiaD

You may use error_reporting = -1
It will always consist of all bits (even if they are not in E_ALL)

您可以使用error_reporting = -1
它总是由所有位组成(即使它们不在 E_ALL 中)

回答by Jan Krüger

In newer PHP versions, E_ALL includes more classes of errors. Since PHP 5.3, E_ALL includes everything exceptE_STRICT. In PHP 6 it will alledgedly include even that. This is a good hint: it's better to see more error messages rather than less.

在较新的 PHP 版本中,E_ALL 包含更多类别的错误。自 PHP 5.3 起,E_ALL 包括E_STRICT之外的所有内容。在 PHP 6 中,据称它甚至会包含这一点。这是一个很好的提示:最好看到更多的错误消息而不是更少。

What's included in E_ALL is documented in the PHP predefined constantspage in the online manual.

E_ALL 中包含的内容记录在在线手册的PHP 预定义常量页面中。

Personally, I think it doesn't matter all that much if you use E_STRICT. It certainly won't hurt you, especially since it may prevent you from writing scripts that have a small chance of getting broken in future versions of PHP. On the other hand, in some cases strict messages may be too noisy, perhaps especially if you're in a hurry. I suggest that you turn it on by default and turn it off when it gets annoying.

就个人而言,我认为如果您使用 E_STRICT 并没有那么重要。它当然不会伤害您,尤其是因为它可能会阻止您编写脚本,这些脚本在未来的 PHP 版本中可能会被破坏。另一方面,在某些情况下,严格的消息可能过于嘈杂,尤其是在您赶时间的情况下。我建议你默认打开它,当它很烦人时关闭它。

回答by stormlash

Depending on your long term support plans for this code, debugging with E_STRICTenabled may help your code to continue working in the distant future, but it is probably overkill for day-to-day use. There are two important things about E_STRICTto keep in mind:

根据您对此代码的长期支持计划,E_STRICT启用调试可能有助于您的代码在遥远的将来继续工作,但对于日常使用来说可能是过度的。有两件重要的事情E_STRICT要记住:

  1. Per the manual, most E_STRICTerrors are generated at compile time, not runtime. If you are increasing the error level to E_ALLwithin your code (and not via php.ini), you may never see E_STRICTerrors anyway.
  2. E_STRICTis contained within E_ALLunder PHP 6, but not under PHP 5. If you upgrade your server to PHP6, and have E_ALLconfigured as described in #1 above, you will begin to see E_STRICTerrors without requiring any additional changes on your part.
  1. 根据手册,大多数E_STRICT错误是在编译时生成的,而不是运行时。如果您将错误级别增加到E_ALL您的代码内(而不是通过php.ini),您可能永远不会看到E_STRICT错误。
  2. E_STRICT包含在E_ALLPHP 6 下,但不包含在 PHP 5 下。如果您将服务器升级到 PHP6,并E_ALL按照上述 #1 所述进行配置,您将开始看到E_STRICT错误,而无需您进行任何其他更改。

回答by Pablo Borowicz

Not strictly speaking of error_reporting, I'd stronglysuggest using any IDE that automatically shows parsing errors and common glitches (eg, assignment in condition).

不是严格地说 error_reporting,我强烈建议使用任何自动显示解析错误和常见故障(例如,条件分配)的 IDE。

Zend Studio for Eclipse has this feature enabled by default, and since I started using it, it has been helping me a lotat catching errors before they occur.

的Zend Studio for Eclipse可以在默认情况下启用此功能,因为我开始使用它,它一直在帮助我很多发生之前就在捕捉错误。

For example, I had this piece of code where I was caching some data in the $GLOBALSvariable, but I inadvertently wrote $_GLOBALSinstead. The data never got cached up, and I'd never knew if Zend didn't tell me: "Hey, this $_GLOBALSthingy appears only once, that might be an error".

例如,我有一段代码,我在$GLOBALS变量中缓存了一些数据,但我无意中写了$_GLOBALS。数据从未被缓存,如果 Zend 没有告诉我:“嘿,这个$_GLOBALS东西只出现一次,那可能是一个错误” ,我永远不会知道。

回答by Tim Boland

ini_set("display_errors","2"); ERROR_REPORTING(E_ALL);

ini_set("display_errors","2"); ERROR_REPORTING(E_ALL);