在php中使用@而不是isset
在某些情况下,我可以使用 @
字符而不是使用较长的 isset()
函数吗? 如果没有,为什么不呢?
我喜欢使用它,因为在很多情况下我可以保存几个引号、括号和点。
In some conditions, may I use an @
character instead of using the longer isset()
function?
If not, why not?
I like to use this because in a lot cases I can save several quotation marks, brackets and dots.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我假设您正在谈论 错误抑制运算符比如说
@
字符,但这并不能替代isset()
。isset()
用于确定给定变量是否已存在于一个程序,以确定使用该变量是否安全。我怀疑您正在做的是尝试使用该变量,无论它是否存在,但抑制可能由此产生的任何错误。在行的开头使用
@
运算符告诉 PHP 忽略任何错误并且不报告它。@
运算符是“临时设置 < 的简写此表达式的 code>error_reporting(0)”。 isset() 是一个完全不同的构造。I assume you're talking about the error suppression operator when you say
@
character, but that isn't a replacement forisset()
.isset()
is used to determine whether or not a given variable already exists within a program, to determine if it's safe to use that variable.What I suspect you're doing is trying to use the variable regardless of its existance, but supressing any errors that may come from that. Using the
@
operator at the beginning of a line tells PHP to ignore any errors and not to report it.The
@
operator is shorthand for "temporarily seterror_reporting(0)
for this expression".isset()
is a completely different construct.您不应该只使用
@
。@
抑制警告。这并不意味着代码是正确的,并且根据您的设置,警告可能仍会添加到您的日志文件中。最好使用 isset 来进行检查。You shouldn't just use an
@
. The@
suppresses warnings. It doesn't mean the code is correct, and warnings might still get added to your log file depending on your settings. It is much better to useisset
to do the check.据我所知,
@
不能替代isset()
。它是一个错误抑制运算符,可以防止在脚本中确实存在错误时显示错误。如果在 PHP 代码中使用,这也是一个非常坏的习惯。As far as I know
@
is no substitution forisset()
. Its an error suppression operator which prevents displaying errors in case they do exist in the script. Its also a pretty bad habit if used in PHP code.它技术上有效,但有几个原因导致我在创建输出时更喜欢显式
isset
解决方案,我认为这就是您正在做的事情:isset
惯用语。我知道你想做什么。使用@
,要弄清楚你的意图并不那么容易。$user->name
。如果您只是使用错误抑制来查看name
是否已设置,那么如果$user
未定义,您将永远不会收到通知。相反,最好运行isset($user->name)
并且明确,这样,如果$user
未定义,您将收到错误通知。It technically works, but there are a few reasons why I prefer the explicit
isset
solution when creating output, which I assume is what you're doing:isset
idiom. I know what you're trying to do. With@
, it's not so easy to figure out your intention.$user->name
. If you just use error suppression to see ifname
is set, you will never be notified if$user
is undefined. Instead, it's better to runisset($user->name)
and be explicit, so that, if$user
is undefined, you will be notified of the error.这取决于您想要做什么。例如,如果您正在执行 var_dump() 或其他调试,并且知道有时您的值不会被设置,我会说在这种情况下这是可以的。
如果您在这种情况下使用它:
我强烈建议您不要这样做。您应该编写代码来明确执行您想要执行的操作。
在生产中我可以考虑使用 @ 的唯一实例是当你想抛出自己的错误时。例如
Also, look at PaulPRO's answer. If what he is saying is indeed true, your log files could also be logging warnings that you don't know about. This would result in your log files being less helpful during debugging after release.
It depends on what you are trying to do. For instance, if you are performing a var_dump() or other debugging and know that sometimes your value will not be set I'd say in this situation it is ok.
If you are using it in this case:
I would strongly advise against it. You should write your code to do explicitly what you want to do.
The only instance in production I can think about using @ is when you want to throw your own error. For example
Also, look at PaulPRO's answer. If what he is saying is indeed true, your log files could also be logging warnings that you don't know about. This would result in your log files being less helpful during debugging after release.
如果没有其他原因,不要使用 @ 代替 isset,因为:
看看这段代码:
如果 $test 是
'something'
那么你会获取'某物'
。如果 $test 为空、
null
或不存在,那么您将得到'default'
;现在问题来了:
假设
'0'
或FALSE
是有效答案?如果 $test 是
'0'
或FALSE
那么您将得到'default'
NOT'0'
作为你会想要的。您应该使用长格式三元:
不需要更多编码,并且在处理可评估为布尔值 false 的参数时更可靠。
If for no other reason, don't use @ as a substitute for isset because of this:
Look at this code:
If $test is
'something'
then you'll get'something'
.If $test is empty,
null
or doesn't exist, then you'll get'default'
;Now here's where the problem comes in:
Suppose
'0'
orFALSE
are valid answers?If $test is
'0'
orFALSE
then you'll get'default'
NOT'0'
as you would want.The long-format ternary is what you should use:
Not much more coding, and more reliable when it comes to dealing with arguments that can evaluate as boolean false.
@
运算符还会使代码运行速度变慢,如下所示:http://php.net/manual/en/language.operators.errorcontrol.php
但正如已经指出的那样,如果发生错误,代码的运行速度就会明显变慢。在这种情况下,使用 isset 而不是
@
运算符的代码要快得多,如下所述:http://seanmonstar.com/post/909029460/php-error-suppression-performance
the
@
operator also makes your code run slower, as pointed out here:http://php.net/manual/en/language.operators.errorcontrol.php
But as it's been pointed out, the code only runs measurably slower if an error occurs. In that case, code using isset instead of
@
operator is much faster, as explained here:http://seanmonstar.com/post/909029460/php-error-suppression-performance