问题描述
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($vars[1])
.
You can simply use !empty($vars[1])
here.
这篇关于为什么要同时检查isset()和!empty()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!