PHP:(!$ VAL)如果VS如果(空($ VAL))。有什么区别吗? [英] PHP: if (!$val) VS if (empty($val)). Is there any difference?
本文介绍了PHP:(!$ VAL)如果VS如果(空($ VAL))。有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道有什么区别以下两种情况,推荐哪一款?
$ VAL = 0;如果(!$ VAL){
//真正
}如果(空($ VAL){
//这也是事实
}
解决方案
有一个看的 PHP类型比较表。
如果您检查表,你会发现,所有的情况下,空($ X)
相同!$ X
。所以它归结为处理未初始化的变量。 !$ X
创建一个 E_NOTICE
,而空($ X)
没有。
I was wondering what's the difference the two cases below, and which one is recommended?
$val = 0;
if (!$val) {
//True
}
if (empty($val) {
//It's also True
}
解决方案
Have a look at the PHP type comparison table.
If you check the table, you'll notice that for all cases, empty($x)
is the same as !$x
. So it comes down to handling uninitialised variables. !$x
creates an E_NOTICE
, whereas empty($x)
does not.
这篇关于PHP:(!$ VAL)如果VS如果(空($ VAL))。有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文