Somenath Sinha
Somenath Sinha

Reputation: 1214

How does this bit of code work?

I'm going through Head First PHP and I've found this snippet of code in PHP.

It's supposed to display each row of records from a table containing the first_name, last_name and email.

while($row = mysqli_fetch_array($result)) 
{
    echo $row['first_name'] . ' ' . $row['last_name'] .
    ' : ' . $row['email'] . '<br />';
}

The $row array contains the next row obtained by mysqli_fetch_array(). But how is this a condition to be checked by the while loop? What exactly does the while loop evaluate to be true/false, before running the inner code? And why exactly does the loop stop when the rows have exhausted? There isn't even a condition to check for an EOF!. So how exactly does this work?

Upvotes: 1

Views: 30

Answers (1)

amhokies
amhokies

Reputation: 785

Referring to the PHP Documenation, mysqli_fetch_array() "Returns an array of strings that corresponds to the fetched row or NULL if there are no more rows in resultset." So, when there are no more results, it returns null which evaluates to false and ends the loop.

Upvotes: 1

Related Questions