Precedencia de operadores

La precedencia de un operador indica qué tan "estrechamente" se unen dos expresiones juntas. Por ejemplo, en la expresión 1 + 5 * 3 , la respuesta es 16 y no 18 porque el operador de multiplicación ("*") tiene una precedencia mayor que el operador de adición ("+"). Los paréntesis pueden ser usados para forzar la precedencia, si es necesario. Por ejemplo: (1 + 5) * 3 se evalúa como 18.

Cuando los operadores tienen igual precedencia su asociatividad decide cómo se agrupan. Por ejemplo "-" tiene asociatividad a izquierda, así 1 - 2 - 3 se agrupa como (1 - 2) - 3 y se evalúa a -4. "=", por otra parte, tiene asociatividad a derecha, así $a = $b = $c se agrupa como $a = ($b = $c).

Los operadores de igual precedencia que no son asociativos no pueden usarse unos junto a otros, por ejemplo, 1 < 2 > 1 es ilegal en PHP. La expresión 1 <= 1 == 1, por otro lado, es legal, ya que el operador == tiene menos precedencia que el operador <=.

El uso de paréntesis, incluso cuando no es estrictamente necesario, a menudo puede aumentar la legibilidad del código haciendo grupos explícitamente en lugar de confiar en la precedencia y asociatividad implícitas del operador.

La siguiente tabla enumera los operadores en orden de precedencia, con los de más alta precedencia al inicio. Los operadores en la misma línea tienen igual precedencia, en cuyo caso la asociatividad decide el agrupamiento.

Precedencia de operadores
Asociatividad Operadores Información adicional
no asociativo clone new clone and new
izquierda [ array()
derecha ** aritmética
derecha ++ -- ~ (int) (float) (string) (array) (object) (bool) @ tipos e incremento/decremento
no asociativo instanceof tipos
derecha ! lógico
izquierda * / % aritmética
izquierda + - . aritmética y string
izquierda << >> bit a bit
no asociativo < <= > >= comparación
no asociativo == != === !== <> <=> comparación
izquierda & bit a bit y referencias
izquierda ^ bit a bit
izquierda | bit a bit
izquierda && lógico
izquierda || lógico
derecha ?? comparación
izquierda ? : ternario
derecha = += -= *= **= /= .= %= &= |= ^= <<= >>= asignación
izquierda and lógico
izquierda xor lógico
izquierda or lógico

Ejemplo #1 Asociatividad

<?php
$a 
5// (3 * 3) % 5 = 4
// la asociatividad del operador ternario difiere de 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
?>

La precedencia y asociatividad de los operadores solamente determinan cómo se agrupan las expresiones, no especifican un orden de evaluación. PHP no especifica (en general) el orden en que se evalúa una expresión y se debería evitar el código que se asume un orden específico de evaluación, ya que el comportamiento puede cambiar entre versiones de PHP o dependiendo de código circundante.

Ejemplo #2 Orden de evaluación no definido

<?php
$a 
1;
echo 
$a $a++; // podría mostrar 2 o 3

$i 1;
$array[$i] = $i++; // podría establecer el índice a 1 o 2
?>

Ejemplo #3 +, - y . tienen la misma precedencia

<?php
$x 
4;
// esta línea podría resultar en una salida inesperada:
echo "x menos uno igual a " $x-", o eso espero\n";
// ya que se evalúa como esta línea:
echo (("x menos uno igual a " $x) - 1) . ", o eso espero\n";
// la precedencia deseada se puede forzar con paréntesis:
echo "x menos uno igual a " . ($x-1) . ", o eso espero\n";
?>

El resultado del ejemplo sería:

-1, o eso espero
-1, o eso espero
x menos uno igual a 3, o eso espero

Nota:

Aunque = tiene una precedencia menor que la mayoría de los demás operadores, PHP aun permitirá expresiones similares a lo siguiente: if (!$a = foo()), en cuyo caso el valor devuelto de foo() es puesto en $a.

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