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

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

Reply via email to