忽略错误是个好主意吗?(这是一个提问标题,无需回答)

4
我发现在我的系统中“error_reporting”被关闭了。所以我将其打开(E_ALL),现在出现了很多错误。
如果你对我的错误感兴趣:
Notice: Undefined index: page in …path/file.php on line 22
Notice: Undefined offset: 1 in …path/file.php on line 49
Notice: Undefined offset: 2 in …path/file.php on line 57
Notice: Undefined offset: 3 in …path/file.php on line 58
Notice: Undefined variable: out in …path/file.php on line 85
Notice: Undefined variable: out in …path/file.php on line 109
Notice: Use of undefined constant M_DESCRIPTION - assumed 'M_DESCRIPTION' in …path/file.php on line 181
Notice: Use of undefined constant GA_TRACKER - assumed 'GA_TRACKER' in …path/file.php on line 291
Notice: A session had already been started - ignoring session_start() in …path/file.php on line 12
Notice: Undefined variable: attributes in …path/file.php on line 86
Notice: Undefined variable: li in …path/file.php on line 129
Notice: Undefined index: breakafterlabel in …path/file.php on line 175
Notice: Undefined index: afterlabel in …path/file.php on line 167
Notice: Undefined index: attributes in …path/file.php on line 188
Notice: Undefined index: value in …path/file.php on line 191
Notice: Undefined index: for in …path/file.php on line 163
Notice: Undefined index: attributes in …path/file.php on line 249
Notice: Undefined index: value in …path/file.php on line 299
Notice: Undefined variable: out in …path/file.php on line 109
Notice: Undefined offset: 0 in …path/file.php on line 418
Notice: Undefined index: maxlength in …path/file.php on line 368
Notice: Undefined index: accept in …path/file.php on line 372
Notice: Undefined variable: out in …path/file.php on line 93
Notice: Undefined index: accept in …path/file.php on line 378
Notice: Undefined index: title in …path/file.php on line 379
Notice: Undefined index: accept in …path/file.php on line 402
Notice: Undefined index: fp in …path/file.php on line 624
Notice: Undefined variable: alert_msg in …path/file.php on line 246
Notice: Undefined variable: returner in …path/file.php on line 87
Notice: Undefined index: body in …path/file.php on line 309
Notice: Undefined variable: out in …path/file.php on line 81
Notice: Undefined variable: defaults in …path/file.php on line 121

一开始我想:“哦,最好再把它关掉”,但我不确定后果!

所以非常简单的问题是:如果我忽略所有错误,会有影响吗?


11
你应该修复它们。 - Carlos Campderrós
3
这些只是通知。当你做了一些“有效”,但不应该这样做的事情时,就会出现通知。例如,如果你在没有先执行 $myArray = array() 的情况下执行 $myAarray[] = 1,你将在第一次访问时收到有关未定义变量 $myArray 的通知。这并不是关键问题,只是代码不完整/混乱。不解决它的后果是什么?你会产生大量日志条目,并且在搜索真正的错误时,概览会受到影响。 - dognose
1
使用 isset 可以修复大部分这些错误。 - andy
1
@dognose 不仅会让人感到烦恼并导致大量日志文件:通知是很好的提示,你可能存在一个bug。它们从解释器的角度来看并不是关键问题,但从应用程序的角度来看可能是的 ;) - KingCrunch
显示剩余3条评论
4个回答

5
在开发环境中最好使用 error_reporting(E_ALL),这样你就可以看到通知。这鼓励你有更高的编码标准。
在运行现场网站时,必须关闭 display_errors,但记录错误(就像你正在做的那样)。
这样,你只会看到有意义的错误,而不是仅仅看到 "Notice: Undefined ..."。如果网站已经完成,你应该花一些时间重构你的代码,长期的收益将是值得的。

1
在生产环境中,我更喜欢使用 error_reporting(E_ALL)display_errors = Offlog_errors = Onerror_log = /some/file/errors.log - Carlos Campderrós

3
如果您只是想暂时隐藏一些错误信息,请使用 @ 符号:
echo @$undefined;
@session_start;

这不会解决你的错误,但在你修复错误时可以使错误列表更清晰。


1

答案是:不行! 忽略错误可能会导致意外的行为。此外,它可能会减慢您程序/脚本的执行速度。因此,最好不要忽略而是修复它们。

更详细地说:在大多数情况下,Undefined offset 错误并不成问题,但 Use of undefined constant 常量或失败的 session_start 可能是危险的。


1
如果你记录它们,你可能会遇到这样的问题:2bits.com他们说:“故事的寓意是:首先不要有 PHP 错误和通知。” - mosch

-1

你应该注意错误和警告信息。 它们告诉你在编程中做错了什么。

在开发环境中,错误信息非常有用。 但是当你发布代码时,最好禁用它们。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接