#### The Internet belongs to everyone. Let’s keep it that way.

Protect Net Neutrality

### Present Remotely

Send the link below via email or IM

• Invited audience members will follow you as you navigate and present
• People invited to a presentation do not need a Prezi account
• This link expires 10 minutes after you close the presentation

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

# Operators precedence in PHP

No description
by

## Max Bodnar

on 23 November 2013

Report abuse

#### Transcript of Operators precedence in PHP

Operators precedence in PHP - tips and tricks
Have you ever seen expressions like these:
\$res = mysql_connect(...) or die();
or
\$a = \$b += 3;
or
if (!\$a = foo()){
.....
}

Precedence
Easy example
That was obvious, what about tricky one?
\$a = 1;
\$b = 4;
\$a += \$b = 5;
echo \$a;
Other tricky thing
Things can be even more confusing
\$a = 1;
\$c = \$a + \$a++;
var_dump(\$c); // int(3)

\$a = 1;
\$c = \$a + \$a + \$a++;
var_dump(\$c); // int(3)
Different operators have different precedence in PHP
\$a = 4 + 3 * 2;
echo \$a;
// 10
Multiplication/division (*, /) operators have higher precedence than addition/subtraction operators (+, -). And assignment operator (=) has lower precedence then two mentioned groups.
So we can say that PHP groups operations like this: \$a = (4 + (3 * 2))
Here we have operators with equal precedence. For such cases there is also associativity set for PHP operators to decide the order of executing command.
Hint: these operators has right associativity.
\$a = 1;
\$b = NULL;
\$c = isset(\$a) && isset(\$b);
\$d = isset(\$a) and isset(\$b);
var_dump(\$c);
var_dump(\$d);
Getting confused with logical operators
Surprise!
var_dump(\$c);
// false;
var_dump(\$d);
// true
What should we see?
Why was that?
Nevertheless both '&&' and 'and' are logical operators that result in AND-ing two variables the precedences of the two are different. More interesting, the precedence of AND is lower then '='.
We can rewrite those expressions as following:
\$c = ( isset(\$a) && isset(\$b) );
( \$d = isset(\$a) ) and isset(\$b);
// in the first expression
\$a + \$a++;
// "++" has higher precedence than "+", so "\$a++" is grouped:
\$a + (\$a++);

// in the second expressions
\$a + \$a + \$a++;
// "++" again has higher precedence than "+":
\$a + \$a + (\$a++);
// and "+" is a left-associative operator, so the left "+" is grouped:
(\$a + \$a) + (\$a++);
Precedences and associativity determines only how operators are grouped. And as soon as they are grouped the groups can be executed in any order.
Parenthesize defensively!
\$a = 1;
\$b = 4;
\$a += \$b = 5;
echo \$a;
// 6

Operators = and += have same precedence but the associativity for them goes from the right to the left. So first we have \$b assigned to 5 and then \$b is added to \$a.
References
http://php.net/manual/en/language.operators.precedence.php
https://gist.github.com/nikic/6699370
Full transcript