Skip to content

PHP Tutorial Operators

Operator Precedence

Lesson 57 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

When an expression has several operators, PHP follows an order of priority called precedence. You know it from maths: multiplication comes before addition.

index.php
PHP
<?php
echo 2 + 3 * 4;
Output
OUTPUT
14

3 * 4 runs first, then + 2.

A simplified order, highest first

  1. ** (power)
  2. ++ -- and casts like (int)
  3. ! (not)
  4. * / %
  5. + -
  6. . (concatenation, since PHP 8)
  7. < <= > >=
  8. == != === !== <=>
  9. &&
  10. ||
  11. ??
  12. ? : (ternary)
  13. = += -= and other assignments
  14. and
  15. xor
  16. or

You do not need to memorise this. Use it when something behaves unexpectedly.

Common surprises

index.php
PHP
<?php
var_dump(true || false && false);   // && is stronger than ||
var_dump((true || false) && false);
Output
OUTPUT
bool(true)
bool(false)
index.php
PHP
<?php
echo -2 ** 2;     // power is stronger than minus
echo "\n";
echo (-2) ** 2;
Output
OUTPUT
-4
4
index.php
PHP
<?php
echo "Sum: " . 1 + 2;   // in PHP 8, + comes before the dot
Output
OUTPUT
Sum: 3

Same level: which side first?

Most operators with the same priority run left to right. A few run right to left: **, assignment and ??.

index.php
PHP
<?php
echo 2 ** 3 ** 2 . "\n";   // 2 ** (3 ** 2)
echo 10 - 4 - 3;           // (10 - 4) - 3
Output
OUTPUT
512
3

The practical rule

Add brackets whenever there is any doubt. Brackets cost nothing, and they tell the next reader exactly what you meant.

index.php
PHP
<?php
$price = 100;
$discount = 20;
$tax = 18;

$total = ($price - $discount) * (1 + $tax / 100);

echo $total;
Output
OUTPUT
94.4
lightbulb
Brackets are not a sign of weakness
Professionals add brackets to mixed && and || expressions. It is clearer, and it avoids a whole family of bugs.