php - 数组在 php 中被视为 bool 值 true 吗?

标签 php casting type-conversion

我有一个简短的问题。我知道 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 中的计算结果是否为 TRUEif(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/

相关文章:

java - 如何将整数转换为十六进制有符号2的补码:

ios - 将NSUInteger乘以CGFloat有什么结果?

php - HTTP请求和PHP流程

casting - 来自 AnyObject 的 "Dynamic cast failure failure"?至国际

java - 将 java 参数从 String 转换为 LinkedList

使用 TValue 进行 Delphi 接口(interface)转换

templates - 也许输入 D

php - 与 MySql 和 Php 相关

php - 替换字符串 PHP 中最后一次出现的至少双倍换行符 (\n\n)

php - 如何使用 jquery 将类添加到超链接?