Die Operator-Rangfolge legt fest, wie "eng" ein Operator zwei Ausdrücke miteinander verbindet. Zum Beispiel ist das Ergebnis des Ausdruckes 1 + 5 * 3 16 und nicht 18, da der Multiplikations-Operator ("*") in der Rangfolge höher steht als der Additions-Operator ("+"). Wenn nötig, können Sie Klammern setzen, um die Rangfolge der Operatoren zu beeinflussen. Zum Beispiel ergibt: (1 + 5) * 3 18. Ist die Rangfolge der Operatoren gleich, wird links nach rechts Assoziativität benutzt.
Die folgende Tabelle zeigt die Rangfolge der Operatoren, oben steht der Operator mit dem höchsten Rang.
| Assoziativität | Operator | 
|---|---|
| keine Richtung | new | 
| rechts | [ | 
| rechts | ! ~ ++ -- (int) (float) (string) (array) (object) @ | 
| links | * / % | 
| links | + - . | 
| links | << >> | 
| keine Richtung | < <= > >= | 
| keine Richtung | == != === !== | 
| links | & | 
| links | ^ | 
| links | | | 
| links | && | 
| links | || | 
| links | ? : | 
| rechts | = += -= *= /= .= %= &= |= ^= <<= >>= | 
| rechts | |
| links | and | 
| links | xor | 
| links | or | 
| links | , | 
Hinweis:
Obwohl ! einen höheren Rang gegenüber = hat, erlaubt es Ihnen PHP immer noch ähnliche Ausdrücke wie den folgenden zu schreiben: if (!$a =foo()).In diesem Ausdruck wird die Ausgabe von foo() der Variablen $a zugewiesen.
Be careful of the difference between
<?php
$obj = new class::$staticVariable();
?>
<?php
$value = class::$staticVariable();
?>
In the first case, the object class will depend on the static variable class::$staticVariable, while in the second case it will be invoked the method whose name is contained in the variable $staticVariable.
Although example above already shows it, I'd like to explicitly state that ?: associativity DIFFERS from that of C++. I.e. convenient switch/case-like expressions of the form
$i==1 ? "one" :
$i==2 ? "two" :
$i==3 ? "three" :
"error";
will not work in PHP as expected
You can use the "or" and "and" keywords' lower precedence for a bit of syntax candy:
<?php
$page = (int) @$_GET['page'] or $page = 1;
?>