PHP计数数组元素

ben*_*e89 2 php arrays count

嗨,有人可以解释为什么这将返回"一个数组由0个元素组成." :

$arr = array(1,3,5); 
$count = count($arr); 
if ($count = 0) { echo "An array is empty."; } else { echo "An array has $count elements."; }
Run Code Online (Sandbox Code Playgroud)

顺便说一句,这是一个我正在进行的测验,我不知道为什么这是正确的答案?

jon*_*ohn 8

$count在条件语句中指定为0

代替...

if ($count = 0)
Run Code Online (Sandbox Code Playgroud)

做这个

if ($count === 0)
Run Code Online (Sandbox Code Playgroud)

  • 是的,请查看此处的文档以获取完整的解释... http://php.net/manual/en/language.operators.comparison.php (4认同)