Operator-Rangfolge

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.

Haben Operatoren die gleiche Rangfolge, dann entscheidet ihre Assoziativität wie die Operatoren gruppiert werden. Zum Beispiel ist "-" links-assoziativ, so dass 1 - 2 - 3 als (1 - 2) - 3 gruppiert und zu -4 ausgewertet wird. Andererseits ist "=" rechts-assoziativ, so dass $a = $b = $c als $a = ($b = $c) gruppiert wird.

Operatoren gleicher Rangfolge, die nicht-assoziativ sind, können nicht nebeneinander verwendet werden; beispielsweise ist 1 < 2 > 1 in PHP nicht erlaubt. Der Ausdruck 1 <= 1 == 1 ist allerdings erlaubt, weil der == Operator eine kleinere Rangfolge als der < Operator hat.

Die Verwendung von Klammern, auch wenn sie nicht unbedingt erforderlich sind, kann oft die Lesbarkeit des Codes verbessern, indem explizit gruppiert wird, statt sich auf die implizite Operator-Rangfolge und -Assoziativität zu verlassen.

Die folgende Tabelle zeigt die Rangfolge der Operatoren, oben steht der Operator mit dem höchsten Rang. Operatoren in derselben Zeile haben die gleiche Rangfolge, so dass ihre Assoziativität die Gruppierung entscheidet.

Operator-Rangfolge
Assoziativität Operator Additional Information
(n. z.) clone new clone und new
rechts ** Arithmetik
(n. z.) ++ -- ~ (int) (float) (string) (array) (object) (bool) @ Typen und Inkrement/Dekrement
left instanceof Typen
(n. z.) ! Logik
links * / % Arithmetik
+ - . Arithmetik und Zeichenketten
links << >> Bitoperatoren
nicht-assoziativ < <= > >= Vergleiche
nicht-assoziativ == != === !== <> <=> Vergleiche
links & Bitoperatoren und Referenzen
links ^ Bitoperatoren
links | Bitoperatoren
links && Logik
links || Logik
rechts ?? NULL-Zusammenfügungsoperator
links ? : ternärer Operator
rechts = += -= *= **= /= .= %= &= |= ^= <<= >>= ??= Zuweisung
(n. z.) yield from yield from
(n. z.) yield yield
(n. z.) print print
links and Logik
links xor Logik
links or Logik

Beispiel #1 Assoziativität

<?php
$a 
5// (3 * 3) % 5 = 4
// die Assoziativität des ternären Operators ist anders als bei C/C++
$a true true 2// (true ? 0 : true) ? 1 : 2 = 2

$a 1;
$b 2;
$a $b += 3// $a = ($b += 3) -> $a = 5, $b = 5
?>

Operator-Rangfolge und -Assoziativität bestimmen nur wie Ausdrücke gruppiert werden, aber nicht die Auswertungsreihenfolge. PHP legt (im Allgemeinen) nicht fest, in welcher Reihenfolge ein Ausdruck ausgewertet wird, und Code, der eine bestimmte Auswertungsreihenfolge erwartet, sollte vermieden werden, denn das Verhalten kann sich von Version zu Version ändern, und auch vom umgebenden Code abhängen.

Beispiel #2 Nicht definierte Auswertungsreihenfolge

<?php
$a 
1;
echo 
$a $a++; // gibt entweder 2 oder 3 aus

$i 1;
$array[$i] = $i++; // der Index ist entweder 1 oder 2
?>

Beispiel #3 +, - und . haben dieselbe Rangfolge

<?php
$x 
4;
// diese Zeile könnte eine unerwartete Ausgabe produzieren:
echo "x minus 1 ist gleich " $x-", hoffe ich jedenfalls\n";
// da es wie diese Zeile ausgewertet wird:
echo (("x minus 1 ist gleich " $x) - 1) . ", hoffe ich jedenfalls\n";
// die gewünschte Rangfolge kann durch die Verwendung von Klammern erzwungen werden:
echo "x minus 1 ist gleich " . ($x-1) . ", hoffe ich jedenfalls\n";
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

-1, hoffe ich jedenfalls
-1, hoffe ich jedenfalls
x minus 1 ist gleich 3, hoffe ich jedenfalls

Hinweis:

Obwohl = einen niedrigeren Rang als die meisten anderen Operatoren hat, erlaubt PHP dennoch Ausdrücke wie den folgenden: if (!$a = foo()); in diesem Fall wird der Rückgabewert von foo() der Variablen $a zugewiesen.

add a note add a note

User Contributed Notes 8 notes

up
167
fabmlk
8 years ago
Watch out for the difference of priority between 'and vs &&' or '|| vs or':
<?php
$bool
= true && false;
var_dump($bool); // false, that's expected

$bool = true and false;
var_dump($bool); // true, ouch!
?>
Because 'and/or' have lower priority than '=' but '||/&&' have higher.
up
27
aaronw at catalyst dot net dot nz
6 years ago
If you've come here looking for a full list of PHP operators, take note that the table here is *not* complete. There are some additional operators (or operator-ish punctuation tokens) that are not included here, such as "->", "::", and "...".

For a really comprehensive list, take a look at the "List of Parser Tokens" page: http://php.net/manual/en/tokens.php
up
46
Carsten Milkau
11 years ago
Beware the unusual order of bit-wise operators and comparison operators, this has often lead to bugs in my experience. For instance:

<?php if ( $flags & MASK  == 1) do_something(); ?>

will not do what you might expect from other languages. Use

<?php if (($flags & MASK) == 1) do_something(); ?>

in PHP instead.
up
7
ivan at dilber dot info
7 years ago
<?php
// Another tricky thing here is using && or || with ternary ?:
$x && $y ? $a : $b// ($x && $y) ? $a : $b;

// while:
$x and $y ? $a : $b// $x and ($y ? $a : $b);

?>
up
4
karlisd at gmail dot com
8 years ago
Sometimes it's easier to understand things in your own examples.
If you want to play around operator precedence and look which tests will be made, you can play around with this:

<?php
function F($v) {echo $v." "; return false;}
function
T($v) {echo $v." "; return true;}

IF (
F(0) || T(1) && F(2)  || F(3)  && ! F(4) ) {
  echo
"true";
} else echo
" false";
?>
Now put in IF arguments f for false and t for true, put in them some ID's. Play out by changing "F" to "T" and vice versa, by keeping your ID the same. See output and you will know which arguments  actualy were checked.
up
-1
anisgazig at gmail dot com
3 years ago
Three types of operator associativity in php.
1.left
2.rigt
3.non-associativity

Category of three operators are right associativity
1)**
2)=,+=,-=,*=,/=,%=,&=,^=,|=,<<=,>>=,??=,.=
3)??

Category of eight operators are non-associativity
1)clone new
2)++,--,~,@
3)!
4)<,<=,>,>=
5)<<,>>
6)yield from
7)yield
8)print

Rest of the operators are left associativity
up
0
noone
4 years ago
Something that threw me of guard and I hadn't found it mentioned anywhere is if you're looking to asign a value in an if statement condition and use the same value in the said condition and compare it to a different value note the precedence of operators.

if($a=5&&$a==5){
  echo '5';
} else {
  echo 'not 5';
}
//echos  not 5

You'll get a Notice:  Undefined variable: a;
This happens because the expression is treated as
($a=5&&($a==5))
In this case $a was undefined.

Use parentheses to enforce the desired outcome or and instead of &&.
if(($a=5)&&$a==5){ // or $a=5 and $a==5
  echo '5';
} else {
  echo 'not 5';
}

//echos  5

We get no notice!

A use case for this can be a three part condition that first checks if a value is valid, second assigns a new variable based on the first value and then checks if the result is valid.

$ID=100;

if ($ID&&($data=get_table_row_for_ID($ID))&&$data->is_valid()) { //NOTE: assigned $data
// do something with the data
}

If assigning variables in an if condition I recommend adding a comment at the end of the line that such an action took place.
up
-1
instatiendaweb at gmail dot com
3 years ago
//incorrect
$a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2
//Unparenthesized `a ? b : c ? d : e` is not supported. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`
//correct
$a = (true ? 0 : true) ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2

==> correction documentation.
To Top