我有时会看到如下代码:
if(!Somefunction()){
// ...
}else{
// ...
}
我更像是一个java开发人员,我知道上述语句的唯一方法就是函数最后返回true或false。
此格式中使用的每个函数都必须返回true或false吗?或者这是否意味着该功能是否成功执行?
答案 0 :(得分:7)
不,不一定是True
或False
。在PHP中,任何变量都可以转换为boolean。
转换为布尔值时,以下值被视为False
:
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags