downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Arithmetische Operatoren> <Operatoren
Last updated: Fri, 30 Oct 2009

view this page in

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. 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.

Operator-Rangfolge
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 print
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.



Arithmetische Operatoren> <Operatoren
Last updated: Fri, 30 Oct 2009
 
add a note add a note User Contributed Notes
Operator-Rangfolge
Patryk Bratkowski
07-Oct-2009 02:18
Please note that certain operators change the variable type in ways you may not expect.

If you want to append the result of an arithmetic operation to a string you have to do it in two steps:

<?php
    $ord
= $i + 1;
   
$field = "cellID" . $ord; // returns a string
   
$field = "cellID" . $i + 1; // because of the addition it returns an int
?>
headden at karelia dot ru
09-Jun-2009 11:02
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
Pies
08-Feb-2009 07:22
You can use the "or" and "and" keywords' lower precedence for a bit of syntax candy:

<?php

$page
= (int) @$_GET['page'] or $page = 1;

?>

Arithmetische Operatoren> <Operatoren
Last updated: Fri, 30 Oct 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites