检查未定义的变量会导致致命错误

发布于 2025-01-30 17:26:08 字数 406 浏览 4 评论 0原文

我将一个非常古老的网站从Symfony 2版本更新为Symfony 6版本,并且正在重写一堆代码。 检查尚未定义的变量时,我正在遇到问题。 当我期望发出简单的警告时,它会导致关键错误。 例如:

if($testvar)
   echo "test";

我希望这会产生警告,但不是致命的错误。我还使用一些PHP工具在线测试了此代码,似乎没有造成致命错误。

如果可以提供帮助,那是我从Symfony遇到的确切错误:

评论家|申请的php异常误解:“警告:未定义变量$ testVar”

我使用的Symfony6与PHP8.1.5的Symfony6

可能会在PHP级别进行某些配置?是否不考虑警告是关键错误?

谢谢

I'm updating a very old website from a symfony 2 version to a symfony 6 version and I'm rewriting a bunch of the code.
I'm encountering a problem when checking a variable that has not be defined.
It causes a critical error when I'm expecting a simple warning.
For example:

if($testvar)
   echo "test";

I expect this to create a Warning, but not a fatal error. I've also tested this code online with some php tools and it seems its not creating a fatal error.

If it can help here is the precise error I'm getting from Symfony:

CRITICA| REQUES Uncaught PHP Exception ErrorException: "Warning: Undefined variable $testvar"

I'm using Symfony6 with PHP8.1.5

Perhaps there is some configuration at PHP level to be done ? Like not considering warning as critical error ?

Thanks

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

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

发布评论

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

评论(1

深海不蓝 2025-02-06 17:26:08

我确实找到了一个简单的解决方案:
更改PHP代码中的错误报告确实是
error_reporting(e_all& 〜e_deprecated& 〜e_strict& 〜e_warning);

我仍然有警告作为日志,但它不再阻止脚本运行

I did find a simple solution:
changing the error reporting inside php code did the trick
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_WARNING);

I still got the warnings as logs, but its not stopping the scripts from running anymore

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