有什么理由要使用isset()吗? [英] Is there any reason to use isset()?
本文介绍了有什么理由要使用isset()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么我应该使用if (isset($var)) {}
而不仅仅是if ($var) {}
?它似乎做同样的事情,只是需要额外的处理.谢谢!
Why should I use if (isset($var)) {}
rather than just if ($var) {}
? It seems to do the same thing and just take extra processing. Thanks!
推荐答案
原因
原因是,isset()
将返回布尔值,并且在检查变量是否存在并继续时不会发出警告.另外,变量可能具有零值:
The reason is, isset()
will return boolean and doesn't throw a warning when you check for the existence of variable and proceed. Also, there is a possibility that, a variable may have zero values:
-
false
-
0
-
""
false
0
""
但是它们已经设置好了.
But they will be already set.
示例
$varb = false;
$vari = 0;
$vars = "";
isset($varb) // true
isset($vari) // true
isset($vars) // true
if ($varb) // false
if ($vari) // false
if ($vars) // false
这篇关于有什么理由要使用isset()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文