关闭 PHP 开发错误日志中的通知是否被视为标准做法

发布于 2024-08-07 09:04:27 字数 133 浏览 4 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

新雨望断虹 2024-08-14 09:04:28

这是不好的做法,但很常见。

您可以说它被认为是标准做法,因为它是开箱即用的默认设置。

然而,它是 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.

静若繁花 2024-08-14 09:04:28

是的,它被认为是标准实践(尽管有好与坏的意见)。 根据 PHP 手册

在 PHP 4 和 PHP 5 中,默认值为 E_ALL & ~E_注意。此设置不显示 E_NOTICE 级别错误。您可能想在开发过程中展示它们。

显示通知可以帮助调试时容易出现打字错误,但我不记得有哪一次我因为没有看到它们而感到痛苦(我是一个很好的打字员)。我想这对于有 Java 背景的人来说会是一个震惊,不过......

Yes, it is considered standard (opinions on good vs. bad notwithstanding) practice. Per the PHP manual:

In PHP 4 and PHP 5 the default value is E_ALL & ~E_NOTICE. This setting does not show E_NOTICE level errors. You may want to show them during development.

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...

最丧也最甜 2024-08-14 09:04:28

它被认为是标准做法,因为它是新版本 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

:)

初懵 2024-08-14 09:04:28

不确定是否如前所述,但本地开发服务器如:

  • Xampp;
  • 兰普;
  • 曼普;
  • 瓦普;

还有许多其他的 PHP 错误设置来报告所有错误和通知。
等于error_reporting(E_ALL);

但是,如果您需要这些快速代码来检查代码中是否有通知或错误,请设置服务器将为特定 PHP 脚本执行的当前错误报告级别:

显示错误但不显示通知:

error_reporting(E_ALL & ~E_NOTICE | E_STRICT);

显示所有内容:

error_reporting(E_ALL);

仅显示错误:

error_reporting(E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR);

您只需在 PHP 脚本开始时添加这些行之一。

PS:在托管服务器中显示通知并不是一个好主意。 请务必在发送到托管服务器时删除这些行


如果您希望更改 PHP.ini 配置中的这些值之一,

。打开 PHP.ini 配置文件,大约 514° 行有默认的错误报告级别。

打开这有帮助。

问候

编辑:这是 514° 线而不是 504°。对不起

Not sure if it as mentioned but local development servers like:

  • Xampp;
  • Lampp;
  • Mampp;
  • Wamp;

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:

error_reporting(E_ALL & ~E_NOTICE | E_STRICT);

Shows Everything:

error_reporting(E_ALL);

Shows only Errors:

error_reporting(E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR);

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

随波逐流 2024-08-14 09:04:28

官方发行版附带的默认 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文