关于函数的返回值和检查变量值,我有一个非常基本的问题。
function test($var1, $var2){
if ($var1 == $var2){
$var3 = "abc";
return $var3;
}
return false
}
$value = test($var1, $var2);
if ($value){
echo "Value is".$value; //should output abc.
} else {
echo "Not equal";
}
if (!empty($value))
还是if (isset($value))
,if ($value)
还是if ($value)
发布于 2011-06-20 08:53:04
我不是PHP开发人员,但我认为您的第一种方法行不通。除了布尔值false之外,还有其他事情被解释为false:
When converting to boolean, the following values are considered 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
http://php.net/manual/en/language.types.boolean.php
发布于 2011-06-20 08:54:51
发布于 2011-06-20 08:53:37
您的函数返回false
,所以我将使用该检查:if ($value != false)
https://stackoverflow.com/questions/6408769
复制相似问题