开发者

PHP Count Array Elements [duplicate]

This question already has answers here: The 3 different equals 开发者_JAVA技巧 (5 answers) Closed 3 years ago.

hi can someone explain why this would return "An array consists of 0 elements." :

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

By the way, this is one a quiz i'm working through and i'm not sure why this is the correct answer?


You are assigning $count to 0 in your conditional statement

Instead of...

if ($count = 0)

Do this

if ($count === 0)


Whenever you are going to check one logic means, check twice if we are using correct operators.

Most of them doing the same mistake like ,

if ($count = 0)   // here you are assigning the value to $count variable

Assign the values instead of compare the values.

if ($count == 0)  // here variable is compared 

We can use more operators for comparing the logical condition.Such like ( >, <, >=, <=, == )

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜