19

the code I'm looking at does this...

while ($info=mysql_fetch_array($data_jurisdiction))
{
//some stuff
}

I'm wondering what does this while statement do? it has an assignment operator within it, so as long as $info gets assigned a value other than false, this code will execute?

0

7 Answers 7

23

[S]o as long as $info gets assigned a value other than false, this code will execute?

Quite, yes. Even there is an assignment operator within that expression, the expression itself still stands for a value. In this case the result of the whole expression is equal to the assignment to $info. In other words: The expression is the same as $info or the expression has been assigned to $info - the last variant is perhaps the best description.

So now whenever $info equals to true, the code block inside while will be executed.

Keep in mind that the comparison is a loose comparison.

So not only false but as well NULL, 0, a zero-length string '' (and it's '0' integer zero counterpart), or an empty array array() ([]) will stop the execution of the inner code-block. ¹


¹ Anything object is commonly non-empty, even (object) NULL or (object) [], however some PHP internal objects (those from extensions) can be true === empty($object) (e.g. an empty SimpleXMLElement (php.net) "<empty/>" or "<empty></empty>" -- but not "<not-empty>0</not-empty>"). But this is merely added for completeness.

Sign up to request clarification or add additional context in comments.

2 Comments

What happens if $info gets assigned 0 ? Does it equate 0 to false and therefore the loop execution stops ?
@user481913: Yes, like with false or NULL or the empty array.
5

For each record $info will be populated with the current row, until it reaches the end of the result set when it will be set to false (which should stop the while loop).

Comments

3

great answer from hakre. what is said is that

while ($info=mysql_fetch_array($data_jurisdiction))

will execute in the same way as this

while (mysql_fetch_array($data_jurisdiction)==true)

or even this

$info = mysql_fetch_array($data_jurisdiction);
if($info==true)

so keep in mind that if mysql_fetch_array($data_jurisdiction) returns anything that can be evaluated to false, the assignment won't work. some of those values are (and I know I will forget a few:

  • 0
  • "0"
  • false
  • "false"
  • NULL
  • ""
  • array() (not fully sure about this one)

Comments

0

as long as $info gets assigned a value other than false, this code will execute?

Yes.

Comments

0

it does loop and stops if $info is false

mysql_fetch_array(); clears row by row so there is new result alltimes

Comments

0

A while loop will execute the nested statement(s) repeatedly, as long as the while expression evaluates to true.

The expression in your example $info = mysql_fetch_object($data_jurisdiction) checks whether the $info, the assigned value from mysql_fetch_object() is equal to true, after type juggling.

It is important to understand two things here:

  1. mysql_fetch_object() returns the next row of the result set until the end of the data set is reached, where it returns false. See the method documentation here.
  2. All assigned values of variables which are not equal to 0, or null evaluate to true after type juggling.

Comments

0

From the manual:

The value of an assignment expression is the value assigned. That is, the value of "$a = 3" is 3.

Also from the manual about mysql_fetch_array:

Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

Therefore, once there are no more rows, the assignment will turn into:

$info = false

Which will get evaluated as false in the while condition, causing the loop to terminate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.