← Back to PHP Course | Chapter 3: Operators | Lesson 8 of 8

PHP Operator Precedence

Operator precedence is the rule about which math step happens first, just like in school where multiplying comes before adding. Brackets let you force the order you want.

Arithmetic Operator Precedence

PHP follows the same math convention you learned in school: * and / are evaluated before + and -, regardless of the order they're written in.

2 + 3 * 4 evaluates the multiplication first and gives 14, not 20 — parentheses are the only way to force a different order.

Example: Arithmetic Operator Precedence

php
<?php
// Print `2 + 3 * 4` to the output
echo 2 + 3 * 4;
?>

Assignment Operator Precedence

Assignment operators sit near the bottom of PHP's precedence table and group right-to-left, which is exactly what makes a chain like $a = $b = $c = 0; work as expected — $c is assigned first, then that same value flows leftward into $b, then $a.

Example: Assignment Operator Precedence

php
<?php
// Declare `$a`, set to `$b = $c = 0`
$a = $b = $c = 0;
// Print "$a $b $c" to the output
echo "$a $b $c";
?>

Logical Operator Precedence

Comparison operators (>, ==, etc.) bind tighter than logical operators (&&, ||), so $age > 18 && $hasId is parsed as ($age > 18) && $hasId without needing explicit parentheses — the comparisons resolve to booleans first, and only then does the logical operator combine them.

Example: Logical Operator Precedence

php
<?php
// Declare `$age`, set to `20`
$age = 20;
// Declare `$hasId`, set to `true`
$hasId = true;
// Print a detailed dump (with types) of `$age > 18 && $hasId`
var_dump($age > 18 && $hasId);
?>

and vs && Precedence

The word-form logical operators (and, or) have noticeably *lower* precedence than their symbolic counterparts (&&, ||) — low enough that $result = false or true; assigns false to $result, because = binds tighter than or.

This exact gotcha is why most style guides steer people toward &&/|| in expressions.

Example: and vs && Precedence

php
<?php
// Declare `$result`, set to `false or true`
$result = false or true;
// Print a detailed dump (with types) of `$result`
var_dump($result);
?>

Left-to-Right Associativity

When two operators share the same precedence level, PHP resolves them left to right — 10 - 3 - 2 evaluates as (10 - 3) - 2, giving 5, not 10 - (3 - 2).

This left-to-right rule is what you fall back on any time precedence alone doesn't fully determine the order.

Example: Left-to-Right Associativity

php
<?php
// Print `10 - 3 - 2` to the output
echo 10 - 3 - 2;
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing $a + $b * $c and expecting left-to-right evaluation, when * binds tighter, so add parentheses for the intended order.
  2. Using and/or in assignments, as in $x = true and false;, where = binds first so $x is true.
  3. Writing !$a instanceof B and expecting it to negate the result of instanceof, when instanceof binds tighter than !, though it is clearer with parentheses.
Chapter Summary
  • PHP has arithmetic, relational, logical, bitwise, and assignment operators.
  • Increment, decrement, and the ternary operator provide short ways to update values or choose a result.
  • Operator precedence decides how a mixed expression is evaluated.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.