Jump to content

reg. while loop with fetch_row


kimdan

Recommended Posts

PHP Freaks forum users,

 

Thanks in advance for your interest in helping.

 

This is my second post, I have general, basic computer programming experience and am beginning PHP.

 

I don't see why the following loop works (PHP), given the variable

$sqli1_dat 

an object taken from a mysqli query. the loop --

 

while( $x = $sqli1_dat->fetch_row() )

{

printf("%s\n", $x[0] );

}

 

this prints column 0. I am aware that fetch_row increments the pointer every time it's called.

 

But I do not follow the loop condition expression. It looks like a variable assignment, like $x = 5? But the loop condition needs to be a truth-value, such as the expression

$x == 5

 

 

Dan

Link to comment
https://forums.phpfreaks.com/topic/297105-reg-while-loop-with-fetch_row/
Share on other sites

It looks like a variable assignment

 

 

yes, it is.

 

 

But the loop condition needs to be a truth-value

 

 

and, yes, it is. it's testing the result of the assignment statement, which will be the value that was assigned. when there are no more rows, the fetch statement returns a false, which will be assigned to the $x variable, and the value tested for the loop condition will be a false and the loop will end.

 

from the documentation for the assignment operator -

The value of an assignment expression is the value assigned. That is, the value of "$a = 3" is 3. This allows you to do some tricky things:

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.