在php中数组是否被认为是布尔值? [英] Is an array considered as boolean true in php?
问题描述
我在这里有一个快速问题。我知道 cakePHP find('first')函数返回一个数组如果找到,则包含第一个结果,否则返回false。我的问题是,如果我要写这样的支票怎么办:
I have a quick question here. I know that the cakePHP find('first') function returns an array containing the first result if found, false otherwise. My question is this, what if I were to write a check like this:
if(result_is_array) // that means I have data
{
// do something
}
else // that means result is a boolean
{
// do something else
}
而不是检查是否从 find('first')获得结果
是不是数组,我可以说:
Instead of checking whether the result obtained from find('first')
is an array or not, can I just say:
$result = $this->MyModel->find('first');
if($result)
{
// do something
}
按顺序,如果我在这里得到一个数组,那在PHP中的计算结果是否为 TRUE
?在PHP中 if(array())
是否等于 true
?
In order words, if I get an array here, will that evaluate to TRUE
in php? Is if(array())
equal to true
in php?
推荐答案
是的,您可以做
$result = $this->MyModel->find('first');
if($result)
长度为的数组0
返回 true
转换为布尔值时,以下值为FALSE
When converting to boolean, the following values are considered FALSE
- 具有零个元素的数组
其他所有值都视为TRUE
Every other value is considered TRUE
这篇关于在php中数组是否被认为是布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!