何时抑制PHP中的错误 [英] When to suppress errors in PHP

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

问题描述

我有一些可能会或可能不会定义的变量(一个 $ isLoggedIn 布尔值),我正在尝试清除错误消息。我想知道是否有任何原因我不应该使用错误抑制操作符:

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)

在生产环境中,这两种方法是否特别有缺点/好处?这两个语句的功能是相同的,并且没有因为未定义此变量而引起的问题。但是,不应将其记录为错误。

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天全站免登陆