PHP Count Array Elements

16,885

You are assigning $count to 0 in your conditional statement

Instead of...

if ($count = 0)

Do this

if ($count === 0)
Share:
16,885
benhowdle89
Author by

benhowdle89

Consultant software engineer and advisor to companies building software products.

Updated on July 16, 2022

Comments

  • benhowdle89
    benhowdle89 almost 2 years

    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?

  • jondavidjohn
    jondavidjohn about 13 years
    yes, check the docs here for a full explanation ... php.net/manual/en/language.operators.comparison.php