Wednesday, 18 January 2017

PHP Count Array Elements




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?


Answer



You are assigning $count to 0 in your conditional statement



Instead of...



if ($count = 0)


Do this



if ($count === 0)

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...