我有一个简短的问题。我知道 cakePHP find('first')如果找到,函数返回包含第一个结果的数组,否则返回 false。我的问题是,如果我写一张这样的支票会怎样:
if(result_is_array) // that means I have data
{
// do something
}
else // that means result is a boolean
{
// do something else
}
与其检查从 find('first')
获得的结果是否是一个数组,我可以说:
$result = $this->MyModel->find('first');
if($result)
{
// do something
}
换句话说,如果我在这里得到一个数组,它在 php 中的计算结果是否为 TRUE
? if(array())
在 php 中是否等于 true
?
最佳答案
是的,你可以做到
$result = $this->MyModel->find('first');
if($result)
length > 0
的数组返回 true
Explanation is here in the docs
When converting to boolean, the following values are considered FALSE
- an array with zero elements
Every other value is considered TRUE
关于php - 数组在 php 中被视为 bool 值 true 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9843077/