confused with statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
global_erp_solution
Forum Commoner
Posts: 25
Joined: Sun Jul 08, 2012 6:47 am

confused with statement

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: confused with statement

Post 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;
global_erp_solution
Forum Commoner
Posts: 25
Joined: Sun Jul 08, 2012 6:47 am

Re: confused with statement

Post by global_erp_solution »

Thank you very much. Now it's more understandable how operator precedence works.
Post Reply