Page 1 of 1

confused with statement

Posted: Fri Jul 27, 2012 10:22 am
by global_erp_solution

Code: Select all

echo $a = $b == $b;
I don't understand this code. Is it comparison or assignment? but it can be printed. Can anybody explain? thanks

Re: confused with statement

Posted: Fri Jul 27, 2012 1:40 pm
by requinix
First comes operator precedence. Because == has higher precedence than =, the statement is equivalent to

Code: Select all

echo $a = ($b == $b);

Code: Select all

echo $a = true;
You can echo/print anything that is an expression, and what is printed is its value. Assignment is an expression and the "value" of it is whatever the assigned value was - in this case true (and not $a). Breaking it down into two statements,

Code: Select all

$a = true;
echo true;

Re: confused with statement

Posted: Fri Jul 27, 2012 11:01 pm
by global_erp_solution
Thank you very much. Now it's more understandable how operator precedence works.