如果我使用未初始化/未定义的变量,有没有办法强制 PHP 报告错误?

发布于 2024-09-17 08:22:23 字数 123 浏览 3 评论 0原文

我将结果与结果混为一谈,犯了一个巨大的错误,我花了大约 4 个小时才最终找到这个 bug。

所以这里的问题是,在 PHP 中,如果我使用未定义/未初始化的变量,我是否可以强制 PHP 报告错误。

谢谢

I made a huge mistake by mixing result with results and it took me around 4 hours to finally find the bug.

So here is the question, in PHP, is it possible that I can enforce PHP to report errors if I use an undefined/uninitialized variable.

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

泪眸﹌ 2024-09-24 08:22:25

它已经报告了一个错误。像这样的事情:

"Notice:  Undefined variable: a in C:\wamp\www\testcenter\index.PHP on line 40"

也许你说得不够具体。但你应该尝试 error_reporting(-1); 就好像强制 php 显示一些建议一样。 php 手册中关于 E_STRICT 错误的一段内容:

启用 PHP 建议对代码进行更改,这将确保代码的最佳互操作性和前向兼容性。

只需记住 error_reporting(-1); 显示的错误比 error_reporting(E_ALL); 更多,因为 E_STRICT 错误不包含在 中E_ALL 约束。

it already does report an error. something like this:

"Notice:  Undefined variable: a in C:\wamp\www\testcenter\index.PHP on line 40"

maybe you didn't go specific enough. but you should try error_reporting(-1); as as if enforces the php to show some recomendations. a piece from the php manual about E_STRICT errors:

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

just remember that error_reporting(-1); shows more errors than error_reporting(E_ALL); because E_STRICT errors are not included in the E_ALL constraint.

我的痛♀有谁懂 2024-09-24 08:22:24

在开发环境中,我更喜欢使用 error_reporting(-1)< /代码>。它报告所有 PHP 错误。

In a development environment I prefer using error_reporting(-1). Which reports all PHP errors.

迷迭香的记忆 2024-09-24 08:22:24

是的,使用 error_reporting() 并将其设置为 E_ALL,如下所示:

 error_reporting(E_ALL);

yes, use error_reporting() and set it to E_ALL, like this:

 error_reporting(E_ALL);
岁吢 2024-09-24 08:22:24

设置错误报告以报告所有错误。在 php.ini 中或在运行时使用 error_reporting(E_ALL)

Set error reporting to report all errors. Either in php.ini or at runtime using error_reporting(E_ALL)

放飞的风筝 2024-09-24 08:22:23

将错误报告设置为 E_ALL 并确保 php.ini 中的 display_errors 已打开。

php.ini

display_errors = On

PHP 代码

// If you cannot access the php.ini file
// you can do this within your PHP code instead
@ini_set('display_errors' '1');
error_reporting(E_ALL);

您现在的默认设置可能不包括通知,PHP 在未初始化的变量上引发的错误类型,可能是这样的:

error_reporting(E_ALL & ~E_NOTICE);

Set error reporting to E_ALL and ensure that display_errors in php.ini is on.

php.ini

display_errors = On

PHP code

// If you cannot access the php.ini file
// you can do this within your PHP code instead
@ini_set('display_errors' '1');
error_reporting(E_ALL);

The default setting you have right now probably excludes notices, the kind of errors PHP raises on uninitialized variables, which could be something like this:

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