Newbie Question about Conditionals

Newbie Question about Conditionals

am 31.03.2010 06:22:50 von Matty Sarro

--000e0cd35214650f31048311191e
Content-Type: text/plain; charset=UTF-8

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'] . ' />';
}

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

--000e0cd35214650f31048311191e--

Re: Newbie Question about Conditionals

am 31.03.2010 06:40:17 von Tommy Pham

On Tue, Mar 30, 2010 at 9:22 PM, Matty Sarro 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'] . ' > />';
> }
>
> 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.

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

Re: Newbie Question about Conditionals

am 31.03.2010 06:45:36 von Matty Sarro

--005045029be9cc4c8e0483116ab8
Content-Type: text/plain; charset=UTF-8

That explains it perfectly, thanks you!

On Wed, Mar 31, 2010 at 12:40 AM, Tommy Pham wrote:

> On Tue, Mar 30, 2010 at 9:22 PM, Matty Sarro 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'] . ' > > />';
> > }
> >
> > 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.
>

--005045029be9cc4c8e0483116ab8--

Re: Newbie Question about Conditionals

am 31.03.2010 10:43:12 von Richard Quadling

On 31 March 2010 05:45, Matty Sarro wrote:
> That explains it perfectly, thanks you!
>
> On Wed, Mar 31, 2010 at 12:40 AM, Tommy Pham wrote:
>
>> On Tue, Mar 30, 2010 at 9:22 PM, Matty Sarro wrote:
>> > Hey all!
>> > This is probably my second post on the list, so please be gentle. Righ=
t
>> now
>> > I am running through the "Heads First PHP and MySQL" book from O'Reill=
y.
>> > 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 gett=
ing
>> > the explanation of how it works.
>> >
>> > Here's the code:
>> >
>> > $result=3Dmysqli_query($dbc,$query)
>> >
>> > while($row=3Dmysqli_fetch_array($result)){
>> > echo $row['first_name'].' '.$row['last_name'].' : '. $row['email'] . '=
>> > />';
>> > }
>> >
>> > 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 alway=
s
>> > 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=3Dmysqli_fetch_array($result)) is equivalent to this:
>>
>> $row=3Dmysqli_fetch_array($result);
>> while ($row){
>>
>> // do something
>>
>> $row=3Dmysqli_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 =3D 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 ===3D ($row =3D 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?



and



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 ...



and



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

By always using ...

while(False !== ($var =3D 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.





--=20
-----
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=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

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

Re: Newbie Question about Conditionals

am 31.03.2010 18:57:02 von Matty Sarro

--001e680f1840a650a904831ba2bf
Content-Type: text/plain; charset=UTF-8

After looking up the === operator, I see exactly what you mean. Thanks for
your help everyone. I think the confusion was that I was always under the
impression that assignment is either true or false; I would never have
guessed it was equal to the value assigned. Your examples really helped to
solidify the concept though!

PS: I have to say that PHP has the *best* documentation I've ever seen
beside Java.

Thanks again!

On Wed, Mar 31, 2010 at 4:43 AM, Richard Quadling
wrote:

> On 31 March 2010 05:45, Matty Sarro wrote:
> > That explains it perfectly, thanks you!
> >
> > On Wed, Mar 31, 2010 at 12:40 AM, Tommy Pham wrote:
> >
> >> On Tue, Mar 30, 2010 at 9:22 PM, Matty Sarro 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'] .
> ' > >> > />';
> >> > }
> >> >
> >> > 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?
>
>
>
> and
>
>
>
> 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 ...
>
>
>
> and
>
>
>
> 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
>

--001e680f1840a650a904831ba2bf--

Re: Newbie Question about Conditionals

am 31.03.2010 19:03:12 von Shawn McKenzie

Matty Sarro 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'] . ' > />';
> }
>
> 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
>

Others have explained in detail, but I will tell you why you and many
beginners may have been confused by this. On this list as well as in
other help sites for PHP, beginners post code that contains something
like this, and wonder why it always echoes TRUE:

$value = 'bob';

if($value = 'test') {
echo 'TRUE';
} else {
echo 'FALSE';
}

They normally get an answer such as: "You need to use == for
comparison, = is an assignment operator and the assignment will always
evaluate to true". Well, this is true for "this" assignment because the
non-empty string 'test' evaluates to true. But without explanation it
may sound as though ANY assignment will evaluate to true just because
the actual assignment itself was successful. This is obviously not the
case. This echoes FALSE:

$value = 'bob';

if($value = '') {
echo 'TRUE';
} else {
echo 'FALSE';
}


--
Thanks!
-Shawn
http://www.spidean.com

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