if($ val)vs if($ val!="")vs。if(!empty($ val)) - 哪一个? [英] if($val) vs. if($val != "") vs. if(!empty($val)) -- which one?
问题描述
我看到很多人使用各种不同的方法来检查变量是否为空,确实似乎没有达成共识。我听说 if($ foo)
与完全一样if(!empty($ foo))
或 if($ foo!=)
。 这是真的吗?
I see a lot of people using a variety of different methods to check whether of a variable is empty, there really seems to be no consensus. I've heard that if($foo)
is exactly the same as if(!empty($foo))
or if($foo != "")
. Is this true?
我意识到这是一个非常简单的问题,但我真的很想知道。有什么不同吗?我应该使用哪种方法?
I realize it's a really simple question, but I'd really like to know. Are there any differences? Which method should I use?
推荐答案
裸测试与空字符串比较的区别
if($ foo!=)
相当于 if($ foo)
大部分时间,但并非总是。
要了解差异的位置,请考虑比较运算符行为以及转换为第一种情况的字符串规则,以及转换为布尔规则。
To see where the differences are, consider the comparison operator behavior along with the conversion to string rules for the first case, and the conversion to boolean rules for the second case.
我发现的是:
- 如果
$ foo === array()
,if($ foo!=)
测试成功(数组是大于字符串),但是if($ foo)
测试将失败(空数组转换为booleanfalse
) - if
$ foo ===0
(一个字符串),if($ foo!=)
test将再次成功(显然),但if($ foo)
测试将失败(字符串0
转换为布尔值false
) - 如果
$ foo
是一个从空标签创建的SimpleXML对象,if($ foo!=)
测试将再次成功(对象是大于字符串),但if($ foo)
test将失败(此类对象转换为booleanfalse
)
- if
$foo === array()
, theif($foo != "")
test will succeed (arrays are "greater than" strings), but theif($foo)
test will fail (empty arrays convert to booleanfalse
) - if
$foo === "0"
(a string), theif($foo != "")
test will again succeed (obviously), but theif($foo)
test will fail (the string"0"
converts to booleanfalse
) - if
$foo
is a SimpleXML object created from an empty tag, theif($foo != "")
test will again succeed (objects are "greater than" strings), but theif($foo)
test will fail (such objects convert to booleanfalse
)
查看行动中的差异 。
See the differences in action.
首选测试方法是 if(!empty($ foo))
,不完全等于上述内容:
The preferred method to test is if(!empty($foo))
, which is not exactly equal to the above in that:
- 它没有受到影响
的不一致如果($ foo!=)
(恕我直言,这很可怕)。 - 它不会产生
E_NOTICE
如果$ foo
在当前范围内不存在,这是它优于的主要优势($ foo)
。
- It does not suffer from the inconsistencies of
if($foo != "")
(which IMHO is simply horrible). - It will not generate an
E_NOTICE
if$foo
is not present in the current scope, which is its main advantage overif($foo)
.
这里有一个警告:if $ foo ==='0'
(长度为1的字符串)然后 empty($ foo)
将返回 true
,通常是(但可能总是)你想要的东西。这也是 if($ foo)
的情况。
There's a caveat here though: if $foo === '0'
(a string of length 1) then empty($foo)
will return true
, which usually is (but may not always be) what you want. This is also the case with if($foo)
though.
最后,当您要测试特定类型的值时,必须对上述内容进行例外处理。例如, strpos
可能会返回 0
,也可能返回 false
。这两个值都将失败 if(strpos(...))
test,但它们的含义完全不同。在这些情况下,使用相同的运营商的测试是有序的: if(strpos()=== false)
。
Finally, an exception to the above must be made when there is a specific type of value you want to test for. As an example, strpos
might return 0
and also might return false
. Both of these values will fail the if(strpos(...))
test, but they have totally different meanings. In these cases, a test with the identical operator is in order: if(strpos() === false)
.
这篇关于if($ val)vs if($ val!="")vs。if(!empty($ val)) - 哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!