Re: Newbie Question about Conditionals
| From: | Tommy Pham | Date: | Wed, 31 Mar 2010 04:40:17 +0000 |
| Subject: | Re: Newbie Question about Conditionals | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-303643@lists.php.net to get a copy of this message | ||
On Tue, Mar 30, 2010 at 9:22 PM, Matty Sarro <msarro@gmail.com> wrote:
> Hey all!
> This is probably my second post on the list, so please be gentle. Right now
> I am running through the "Heads First PHP and MySQL" book from O'Reilly.
> It's been quite enjoyable so far, but I have some questions about some of
> the code they're using in one of the chapters.
>
> Basically the code is retreiving rows from a DB, and I'm just not getting
> the explanation of how it works.
>
> Here's the code:
>
> $result=mysqli_query($dbc,$query)
>
> while($row=mysqli_fetch_array($result)){
> echo $row['first_name'].' '.$row['last_name'].' : '.
> $row['email'] . '<br
> />';
> }
>
> Now, I know what it does, but I don't understand how the conditional
> statement in the while loop works. Isn't an assignment operation always
> going to result in a "true" condition? Even if mysqli_fetch_array($result)
> returned empty values (or null) wouldn't the actual assignment to $row still
> be considered a true statement? I would have sworn that assignment
> operations ALWAYS equated to true if used in conditional operations.
> Please help explain! :)
>
> Thanks so much!
> -Matty
>
http://www.php.net/manual/en/function.mysql-fetch-array.php
"Returns an array of strings that corresponds to the fetched row, or
FALSE if there are no more rows."
while($row=mysqli_fetch_array($result)) is equivalent to this:
$row=mysqli_fetch_array($result);
while ($row){
// do something
$row=mysqli_fetch_array($result);
}
So, if $row is not equal to FALSE, the loop will happens.