On 31 March 2010 05:45, Matty Sarro <msa...@gmail.com> wrote:
> That explains it perfectly, thanks you!
>
> On Wed, Mar 31, 2010 at 12:40 AM, Tommy Pham <tommy...@gmail.com> wrote:
>
>> On Tue, Mar 30, 2010 at 9:22 PM, Matty Sarro <msa...@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.
>>
>

Another part to the answer is the value of the assignment. From the
documentation (http://docs.php.net/manual/en/language.operators.assignment.php),
...

"The value of an assignment expression is the value assigned."

while($row = mysqli_fetch_array($result))


But there is a significant issue here. Whilst not attributable to the
mysqli_fetch_array() function, it is certainly possible to give
yourself a significant WTF moment with it.


May be will be easier to see the problem when the code is written as ...

while(False === ($row = mysqli_fetch_array($result)))

No?


If I say ...

0 == False

does that help?



Without running the 2 stupid scripts  below, what output should you get?

<?php while($pos = strpos('abc', 'a')) { echo 'a';} ?>

and

<?php while($pos = strpos('abc', 'z')) { echo 'z';} ?>

Clearly, they should be different, yes?

Unfortunately, they won't be.

The first call returns 0 and the second returns False.

And as 0 == False, the while() does not loop.

But ...

<?php while(False !== ($pos = strpos('abc', 'a'))) { echo 'a';} ?>

and

<?php while(False !== ($pos = strpos('abc', 'z'))) { echo 'z';} ?>

now operate correctly. The first one runs forever, printing a's and
the second one quits straight away.

By always using ...

while(False !== ($var = function($param)))

you can clearly differentiate between functions that return false to
indicate failure and 0 to indicate a position or actual value.

Regards,

Richard.





-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to