Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

while loop in php with assignment operator

Tags:

php

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?

like image 662
triq Avatar asked Jul 13 '11 15:07

triq


People also ask

What does => mean in PHP?

It means assign the key to $user and the variable to $pass. When you assign an array, you do it like this. $array = array("key" => "value"); It uses the same symbol for processing arrays in foreach statements. The '=>' links the key and the value.

Do While and while do loop with an example in PHP?

Note: In a do... while loop the condition is tested AFTER executing the statements within the loop. This means that the do... while loop will execute its statements at least once, even if the condition is false.


1 Answers

[... 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 or an empty array will stop the execution of the inner code-block.

like image 117
hakre Avatar answered Sep 18 '22 09:09

hakre