什么时候阻止PHP中的错误 [英] When to supress errors in PHP

查看:102
本文介绍了什么时候阻止PHP中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些可能被定义的变量(一个 $ isLoggedIn boolean),我试图清除错误消息。我想知道是否有任何理由我不应该使用错误抑制器操作符:

I've got some variables that may or may not be defined (an $isLoggedIn boolean) and I'm trying to clean out the error messages. I was wondering if there was any reason I shouldn't use the error suppressor operator:

if (@$isLoggedIn)

或者如果我应该首先检查变量的存在:

Or if I should instead check the existance of the variable first:

if (isset($isLoggedIn)and$isLoggedIn)



在生产环境中特别是有任何缺点/好处吗?两个语句的功能是相同的,并且没有这个var是未定义的问题。它不应该被记录为错误。

In a production environment is there any drawback/benefit to either approach in particular? The functionality of both statements is identical and there are no problems caused by this var being undefined. It shouldn't be logged as an error though.

推荐答案

根据我的经验,你不应该在个人层面上抑制错误。在您的生产环境中,将错误报告设置为0并将错误显示为关闭。像这样的个人压制当你在6个月内回来修正一个错误时,就会自动射击自己,忘记它在那里,无法解决为什么没有错误显示出来,但有些东西被明确地破坏了。

In my experience, you should never suppress errors on an individual level. In your production environment set error reporting to 0 and display errors to off. Having individual supressions like this is shooting yourself in the foot when you come back to fix an error in 6 months, forget it's there and can't work out why no errors are showing up but something is clearly broken.

在你的具体情况下,只需将$ isLoggedIn设置为false,然后在当前正在实例化的位置覆盖该值(我在此假设代码结构)。

In you particular case, just set $isLoggedIn to false then override that value at the location you are currently instantiating it (I'm assuming code structure here).

这篇关于什么时候阻止PHP中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆