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

Protect Net Neutrality
Loading presentation...

Present Remotely

Send the link below via email or IM


Present to your audience

Start remote presentation

  • 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
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

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

Max Bodnar

on 23 November 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Operators precedence in PHP

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

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);
Getting confused with logical operators
// false;
// 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.
Full transcript