【发布时间】:2016-07-16 18:30:05
【问题描述】:
我有一个验证函数,如果验证通过,我想返回 true,如果验证失败,我想返回一个错误数组。但是,当我检查函数是否返回 true 时,即使返回值是数组,它也会返回 true。为什么是这样?作为我的意思的另一个例子:
$array = ['test' => 'test'];
if ($array == true)
{
echo 'true';
}
我也尝试了同样的字符串:
$string = 'string';
if ($string == true)
{
echo 'true';
}
两者都回显真实。
这是为什么?如果我们能做到这一点,为什么我们需要 isset() 函数?
【问题讨论】:
-
数组可以使用
count(),如果为0则为false -
if($array === true) 也检查 typeof
-
isset() 将检查变量是否存在,if($variable) 将检查 $variable 值是否为 0 或 false。
-
声明
$variables或空字符串将返回 true。除非您将$variables声明为 false。