while loop in php with assignment operator - 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?

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

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

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)

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

it does loop and stops if $info is false
mysql_fetch_array(); clears row by row so there is new result alltimes

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:
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.
All assigned values of variables which are not equal to 0, or null evaluate to true after type juggling.

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.

Related

Is =! a declaration and is that why this alternating conditional works?

I have a terrible confession. I've been using variations of the below for ages.
for($x=0;10>$x;++$x){
echo '<li style="color:#'.(($c=!$c)? "fff":"eee").'">example $x</li>";
}
My dilemma is that I don't entirely understand how it works. I know that $c=!$c makes the ternary conditional alternate, but I don't understand how. Googling "php =!" and the likes yields nothing helpful (no surprise given the query).
!= is an operator, but this uses =!
Is this a ternary conditional declaration? If so, how does this one work? I understand the general conditional declarations, but not this one... assuming it even is one.
Any answer or link to documentation would be greatly appreciated.
They are two separate operators. You should read it as
$c = !$c;
In other words, assign the result of the expression !$c back to the variable $c. So indeed, it toggles the value of $c.
Additionally, an assignment like this also returns the assigned value, so you can evaluate it immediately. So in words, the expression ($c=!$c)? "fff":"eee" says:
"Invert the value of $c. If the new value is true, return 'fff', otherwise return 'eee'."
It's not an operator, it just needs better spacing:
$x = !$x;
it's just inverting the value.
=! is not an operator. It is two: = means assignment, and ! means negation.
If $c is true, then !$c is false (and vice versa).
And assignments evaluate to their new value.
You're setting $c to be not itself. So if $c == true you are saying $c = not true, I.e. false which will also count as false for the ternary.
Next iteration you set $c = not false I.e. true and so on.

Assignment statement with AND operator

Can any one explain me following construct.
I do googling for this about 2 hours but can't understand.
public function __construct($load_complex = true)
{
$load_complex and $this->complex = $this->getComplex();
}
See: http://www.php.net/manual/en/language.operators.logical.php
PHP uses intelligent expression evaluation. If any of AND's operands evaluates to false, then there is no reason to evaluate other, because result will be false.
So if $load_complex is false there is no need to evaluate $this->complex = $this->getComplex();
This is some kind of workaround, but I do not suggest to use it, because it makes your code hard to read.
Specifically to your example $this->complex = $this->getComplex() if and only if $load_complex is set to true.
LIVE DEMO
NOTE: If any one of OPERAND result becomes 'false' in short
circuit AND evaluation means, the part of statement will be
OMITTED because there is no need to evaluate it.
Dont code like below line because, you may get probably logical
error while you are putting Expression instead of assigning values
to the variable on LEFT HAND SIDE...
$load_complex and $this->complex = $this->getComplex();
I have modified below with conditinal statement for your needs...
if($load_complex and $this->complex) {
$this->getComplex();
}

php conditional operator not transient

I am having a bit of trouble wrapping my head around this, so I thought I will ask it..
I have this code:
$x="string";
var_dump($x==0); //says true
var_dump($x==true); //says true
var_dump(true==0); //says false
What I understand is:
In 1, `string` gets converted to number, which becomes `0` so condition is true
In 2, `string` is a value, so condition is true
In 3, `true` is not equal to `0` so condition is false
Individually they all make sense, but in a sequence they just don't! I have heard many people say it is because the conditional operator in PHP is not transient. Can someone explain what that means, and how to make sense of this?
Why in the world wouldn't that make sense? You never change the value of x, so those statements are dependent on the original value of x which is "string".
You are assuming x is changed to 0 in the first var_dump but it is not. It is merely compared to 0/false. This statement would perform the variable change that you think is happening:
var_dump(($x = 0) == 0);
Or:
var_dump(($x = false) == 0);

Two Ampersands Between Function Calls

What do two ampersands separating function calls do? Note: this is OUTSIDE any if statement or case statement
Like so:
functionCallOne() && functionCallTwo();
it is equivalent to
if ( functionCallOne() ) {
functionCallTwo();
}
it just uses the short circuiting to make this 3 liner only take up one line
That is the short-circuit AND operator, meaning the second function (or expression/statement) will only be evaluated if the first returns true.
The logical AND operator && is a short-circuit operator. That means it will only move on to examine the second operand if the first operand is true. If the first operand is false, the whole expression can only be false, so there's no point in checking the second operand, so it short-circuits out of the operation without having checked the second operand.
In this case this characteristic is used to mean "execute functionCallOne and if that was successful execute functionCallTwo".
The second function (with all it's side-effects) is not even executed if the first one returns false, because PHP knows the whole expression can never be true if one of them is false.

Strange variable assignment

I was studying some code I found on the web recently, and came across this php syntax:
<?php $framecharset && $frame['charset'] = $framecharset; ?>
Can someone explain what is going on in this line of code?
What variable(s) are being assigned what value(s), and what is the purpose of the && operator, in that location of the statement?
Thanks!
Pat
Ah, I just wrote a blog post about this idiom in javascript:
http://www.mcphersonindustries.com/
Basically it's testing to see that $framecharset exists, and then tries to assign it to $frame['charset'] if it is non-null.
The way it works is that interpreters are lazy. Both sides of an && statement need to be true to continue. When it encounters a false value followed by &&, it stops. It doesn't continue evaluating (so in this case the assignment won't occur if $framecharset is false or null).
Some people will even put the more "expensive" half of a boolean expression after the &&, so that if the first condition isn't true, then the expensive bit won't ever be processed. It's arguable how much this actually might save, but it uses the same principle.
&& in PHP is short-circuited, which means the RHS will not be evaluated if the LHS evaluates to be false, because the result will always be false.
Your code:
$framecharset && $frame['charset'] = $framecharset;
is equivalent to :
if($framecharset) {
$frame['charset'] = $framecharset;
}
Which assigns the value of $framecharset as value to the array key charset only if the value evaluates to be true and in PHP
All strings are true except for two: a string containing nothing at all and a string containing only the character 0

Categories