为什么要同时检查 isset() 和 !empty() [英] Why check both isset() and !empty()
问题描述
isset
和 !empty
之间有区别吗?如果我进行双重布尔检查,这种方式是正确的还是多余的?有没有更短的方法来做同样的事情?
Is there a difference between isset
and !empty
. If I do this double boolean check, is it correct this way or redundant? and is there a shorter way to do the same thing?
isset($vars[1]) AND !empty($vars[1])
推荐答案
这完全是多余的.empty
或多或少是 !isset($foo) || 的简写!$foo
和 !empty
类似于 isset($foo) &&$foo
.IE.empty
与 isset
做相反的事情,并额外检查一个值的 真实性.
This is completely redundant. empty
is more or less shorthand for !isset($foo) || !$foo
, and !empty
is analogous to isset($foo) && $foo
. I.e. empty
does the reverse thing of isset
plus an additional check for the truthiness of a value.
或者换句话说,empty
与 !$foo
相同,但如果变量不存在则不会抛出警告.这就是这个函数的要点:做一个布尔比较而不用担心设置的变量.
Or in other words, empty
is the same as !$foo
, but doesn't throw warnings if the variable doesn't exist. That's the main point of this function: do a boolean comparison without worrying about the variable being set.
手册是这样写的:
empty()
与(boolean) var
相反,除了没有设置变量时不会产生警告.
您可以在这里简单地使用 !empty($vars[1])
.
You can simply use !empty($vars[1])
here.
这篇关于为什么要同时检查 isset() 和 !empty()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!