关闭 PHP 开发错误日志中的通知是否被视为标准做法
我有 J2EE 背景,似乎 PHP 开发人员使用以下语句关闭和忽略通知是很常见的: error_reporting(E_ALL & ~E_NOTICE);
我正在使用的应用程序充满了有关未设置变量的消息? 这对我来说似乎很奇怪。
I coming from a J2EE background and it seems that it is very common for PHP developers to turn off and ignore notices with the statement:
error_reporting(E_ALL & ~E_NOTICE);
The application that I'm working is full of messages about unset variables?
This seems very odd to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是不好的做法,但很常见。
您可以说它被认为是标准做法,因为它是开箱即用的默认设置。
然而,它是 PHP 中的默认设置这一事实不应被视为意味着这是一个好主意! (咳嗽
register_globals
< /a> 咳嗽)问题是
E_NOTICE
涵盖未定义的变量和未定义的数组索引,前者比后者更好地指示错误。这个隐藏的经典错误是当您打算使用
$this->var
时却使用了$var
。仅出于这个原因,我认为清理未定义的数组索引警告消息是值得的,以便未定义的变量错误更加明显。我原以为 PHP 5.3 允许你将它们分开(我还没有使用它),但我刚刚查看过,但找不到提及这一点。
It's bad practise, but it's pretty common.
You could say it's considered standard practise since it's the default setting out of the box.
However the fact that it's the default setting in PHP shouldn't be taken as meaning it's a good idea! (cough
register_globals
cough)The problem is that
E_NOTICE
covers both undefined variable and undefined array indexes, the former of which is a far better indication of a bug than the latter.The classic bug that this hides is using
$var
when you meant to use$this->var
. For this reason alone I think it's worth being anal about cleaning up the undefined array index warning messages so that undefined variable bugs are more obvious.I had thought that PHP 5.3 allowed you to separate these (I'm not using it yet), but I've just looked and I can't find mention of this.
是的,它被认为是标准实践(尽管有好与坏的意见)。 根据 PHP 手册:
显示通知可以帮助调试时容易出现打字错误,但我不记得有哪一次我因为没有看到它们而感到痛苦(我是一个很好的打字员)。我想这对于有 Java 背景的人来说会是一个震惊,不过......
Yes, it is considered standard (opinions on good vs. bad notwithstanding) practice. Per the PHP manual:
Displaying notices can help the typo prone with debugging, but I can't think of a time I've ever been bitten in the ass by not seeing them (I'm a good typist). I imagine it comes as a shock for someone with a Java background, though...
它被认为是标准做法,因为它是新版本 PHP 的默认设置。
您可以在使用之前不设置变量,通常会出现一些警告,因此立即决定(因为您知道它不会产生巨大的影响)是关闭它们。
如果你的编程正确,那么在我继承系统之前就不需要
将所有警告设置为关闭,因为修复那里的工作是不可行的。
有关详细信息,请参阅 用于错误报告的 PHP 页面< /a>
:)
It's considered standard practise since it's the default setting now with the new version of PHP.
You can get away with not setting variables before there use it's common to have a handfull of warnings and therefore the immediate decision (as you know its not going to have a huge effect) is to turn them off.
If you program correctly then it shouldn't be required
I've set all the warning to be off before when i inherited a system because it wasn't feasible to fix there work.
For more info here is the PHP page for error reporting
:)
不确定是否如前所述,但本地开发服务器如:
还有许多其他的 PHP 错误设置来报告所有错误和通知。
等于
error_reporting(E_ALL);
。但是,如果您需要这些快速代码来检查代码中是否有通知或错误,请设置服务器将为特定 PHP 脚本执行的当前错误报告级别:
显示错误但不显示通知:
显示所有内容:
仅显示错误:
您只需在 PHP 脚本开始时添加这些行之一。
PS:在托管服务器中显示通知并不是一个好主意。 请务必在发送到托管服务器时删除这些行
如果您希望更改 PHP.ini 配置中的这些值之一,
。打开 PHP.ini 配置文件,大约 514° 行有默认的错误报告级别。
打开这有帮助。
问候
编辑:这是 514° 线而不是 504°。对不起
Not sure if it as mentioned but local development servers like:
And many other have PHP error setting to report all errors and notices.
Equal to
error_reporting(E_ALL);
.However if you ever need these quick codes to check you code for notices or errors, to set the current error report level that the server will perform for a specific PHP script:
Shows Errors but doesn't show Notices:
Shows Everything:
Shows only Errors:
You just have to add one of these lines in your PHP Script on the start.
PS: Not a very good idea to show notices in your hosting server. Be sure to remove these lines when you send then to the hosting server
If your looking to change you PHP.ini config for one of these values.
Open your PHP.ini config file and about the 514º line there is the default error reporting level.
Open this helps.
Regards
EDIT: It was the 514º line not the 504º. Sorry
官方发行版附带的默认 php.ini 文件有点精神分裂。一方面,它声称有利于开发(例如显示错误),但随后它关闭了通知,并附有说明打开它们将有利于开发。 (我实际上提交了一个关于此的错误,但它已关闭,因为他们显然不想修复此问题。)IME,大多数开发人员不会更改此默认值,因为他们大多数人不知道他们可以。因此,我看到很多 PHP 代码都出于这个原因生成通知。
如果您有选择,请打开“通知”。然后去修复那些愚蠢的事情的代码,比如使用未设置的变量等等。你的代码会因此变得更好。
The default php.ini file shipped with the official distribution is a bit schizophrenic. On the one hand, it claims to be good for development (e.g. shows errors) but then it has Notices turned off along with a note that turning them on would be good for development. (I actually filed a bug about this, but it was closed as they clearly didn't want to fix this.) IME, Most developers do not change this default because most of them don't know they can. So I see a lot of PHP code that generates Notices for this reason.
If you have a choice, turn Notices on. Then go and fix code that does daft things like use unset variables and what not. Your code will be better for it.