if(isset($ var))和if($ var)之间的区别 [英] Difference between if(isset($var)) and if($var)
问题描述
Possible Duplicate:
Whats the difference between if(!Variable) and if(isset($variable)) ?
在Facebook的php API示例中,他们使用if($ var){//做某事} else {//做其他事} ick使我开始思考if($ var)和if(isset($ var))的区别.第一个确定的对象看起来更整洁,但是我可以确定使用它吗?
In Facebooks php API example they use if($var){ //do something }else{ //do something else} whick got me thinking about the difference of if($var) and if(isset($var)). The first sure looks neater but can I surely use it?
推荐答案
使用
if ($var)
您将测试$var
是否包含不为false的值-1为true,123也为false ...
You'll test if $var
contains a value that's not false -- 1 is true, 123 is too, ...
For more informations about what is considered as true or false, you should take a look at Converting to Boolean.
使用 isset()
,您将测试是否已设置变量-即是否未设置任何变量-null值已写入其中.
Using isset()
, you'll test if a variable has been set -- i.e. if any not-null value has been written to it.
这篇关于if(isset($ var))和if($ var)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!