srakl
srakl

Reputation: 2619

php list function not echoing variable outside loop

Any idea why i can't echo $a outside the loop? what am i missing?

while (list($a) = $db->fetch_array($query))
{
     $b = $a; //where $a = 10
}

echo $a; //this echo's nothing.
echo $b; //this echo's 10

Upvotes: 0

Views: 76

Answers (1)

bwoebi
bwoebi

Reputation: 23777

$a is reset to NULL when the while loop ends, so it outputs nothing. (and the loop isn't entered at this moment anymore, so $b = $a won't be executed anymore)

This is because $db->fetch_array() returns false when there are no rows left.

Upvotes: 10

Related Questions