PHP是否如果(计数($阵列)),如果($数组)指的是同样的事情? [英] PHP Does if (count($array)) and if ($array) mean the same thing?

查看:118
本文介绍了PHP是否如果(计数($阵列)),如果($数组)指的是同样的事情?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,将这些总是返回相同的值?

In PHP, will these always return the same values?

//example 1

$array = array();

if ($array) {
   echo 'the array has items';

}

// example 2

$array = array();

if (count($array)) {
   echo 'the array has items';

}

感谢您!

推荐答案

从<一个href=\"http://www.php.net/manual/en/language.types.boolean.php\">http://www.php.net/manual/en/language.types.boolean.php,它说,一个空数组被认为是假。

From http://www.php.net/manual/en/language.types.boolean.php, it says that an empty array is considered FALSE.


(引用):
当转换为boolean时,以下值被认为是FALSE:

(Quoted): When converting to boolean, the following values are considered FALSE:


  • 布尔值FALSE

  • 整数0(零)

  • 浮0.0(零)

  • 空字符串,字符串0

  • 具有零元素的数组

  • 零成员变量(PHP 4只)
  • 对象
  • 特殊类型NULL(包括尚未设定的变量)

  • 从空标签创建的SimpleXML对象


由于


  • > 0计数()是不假

  • 填充阵列是不假

然后像预期的那样的问题说明这两种情况下会一直工作。

then both cases illustrated in the question will always work as expected.

这篇关于PHP是否如果(计数($阵列)),如果($数组)指的是同样的事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆