W3docs

PHP Operators

Learn all PHP operator types — arithmetic, comparison, logical, string, array, spaceship, and null-coalescing — with examples and precedence rules.

An operator takes one or more values (called operands) and produces a new value from them. Operators are the building blocks of every expression you write in PHP — they let you do arithmetic, compare values, combine boolean conditions, assign results to variables, and join strings.

This chapter covers every category of PHP operator with runnable examples: arithmetic, assignment, comparison, logical, increment/decrement, string, array, the spaceship operator, the null-coalescing operator, and how operator precedence decides the order of evaluation.

Arithmetic Operators

Arithmetic operators perform the familiar mathematical operations on numbers.

OperatorNameExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 42.5
%Modulo (remainder)10 % 31
**Exponentiation2 ** 416
<?php
$x = 10;
$y = 3;

echo $x + $y;   // 13
echo $x - $y;   // 7
echo $x * $y;   // 30
echo $x / $y;   // 3.3333333333333
echo $x % $y;   // 1   (remainder of 10 / 3)
echo $x ** $y;  // 1000 (10 to the power of 3)

Note: Unlike many languages, PHP's / does not truncate. 10 / 4 is 2.5, not 2. Use intdiv() for integer division. The % operator works on integers; for floating-point remainders use fmod().

Assignment Operators

The basic assignment operator is = — it stores the value on the right into the variable on the left. Each arithmetic operator also has a combined (compound) form that updates a variable in place.

OperatorSame as
$a += $b$a = $a + $b
$a -= $b$a = $a - $b
$a *= $b$a = $a * $b
$a /= $b$a = $a / $b
$a %= $b$a = $a % $b
$a .= $b$a = $a . $b (string append)
<?php
$total = 100;
$total += 20;  // 120
$total -= 50;  // 70
$total *= 2;   // 140
echo $total;   // 140

Comparison Operators

Comparison operators compare two values and return a boolean (true or false). Notice the important distinction between loose (==) and strict (===) comparison.

OperatorNametrue when...
==Equalvalues are equal after type juggling
===Identicalvalues and types are equal
!= / <>Not equalvalues are not equal
!==Not identicalvalues or types differ
<Less thanleft is smaller
>Greater thanleft is larger
<=Less than or equal
>=Greater than or equal
<?php
var_dump(10 == "10");   // bool(true)  — values match after juggling
var_dump(10 === "10");  // bool(false) — int vs string, types differ
var_dump(5 != 8);       // bool(true)
var_dump(5 >= 5);       // bool(true)

Gotcha: Prefer === when you care about types. 0 == "abc" is false in modern PHP (8.0+), but loose comparison still has surprising edge cases — strict comparison avoids them.

The spaceship operator <=>

The spaceship operator compares two values and returns -1, 0, or 1. It is most useful as the return value of a sorting callback.

<?php
echo 1 <=> 2;   // -1  (left is smaller)
echo 2 <=> 2;   //  0  (equal)
echo 3 <=> 2;   //  1  (left is larger)

Logical Operators

Logical operators combine boolean expressions, typically inside if conditions.

OperatorNametrue when...
&& / andAndboth sides are true
|| / orOrat least one side is true
!Notthe operand is false
xorExclusive orexactly one side is true
<?php
$age = 25;
$hasLicense = true;

var_dump($age >= 18 && $hasLicense);  // bool(true)
var_dump($age < 18 || $hasLicense);   // bool(true)
var_dump(!$hasLicense);               // bool(false)

&& and and behave the same in conditions but have different precedenceand/or bind more loosely than =, which can surprise you. Stick to && and ||.

PHP also short-circuits: in $a && expensive(), if $a is false the right side is never evaluated.

Increment and Decrement Operators

These operators add or subtract 1 from a variable. Their position (pre vs post) changes what the expression evaluates to.

OperatorEffect
++$xPre-increment: increment, then return the new value
$x++Post-increment: return the old value, then increment
--$xPre-decrement
$x--Post-decrement
<?php
$x = 5;
echo $x++;  // 5  (prints old value, then $x becomes 6)
echo $x;    // 6
echo ++$x;  // 7  (increments first, then prints)

String Operators

PHP has two operators dedicated to strings:

  • . — the concatenation operator, joins two strings.
  • .= — the concatenating assignment operator, appends to the variable.
<?php
$greeting = "Hello";
$greeting .= ", World!";   // append
echo $greeting;            // Hello, World!
echo "PHP" . " " . "rocks"; // PHP rocks

Array Operators

Array operators work on whole arrays.

OperatorNameDescription
+Unionkeys from the right are added only if missing on the left
==Equalitysame key/value pairs
===Identitysame pairs, same order, same types
<?php
$a = ["a" => 1, "b" => 2];
$b = ["b" => 99, "c" => 3];

print_r($a + $b);
// Array ( [a] => 1 [b] => 2 [c] => 3 )
// "b" keeps the LEFT value (2), "c" is added

Null-Coalescing Operator ??

The ?? operator returns its left operand if it exists and is not null, otherwise the right operand. It is the clean replacement for isset() checks.

<?php
$data = ["name" => "Ann"];

$name = $data["name"] ?? "Guest";  // "Ann"
$role = $data["role"] ?? "Member"; // "Member" — key missing, no warning
echo "$name / $role";              // Ann / Member

The ??= null-coalescing assignment assigns only when the variable is currently null or unset: $config["theme"] ??= "dark";.

Operator Precedence

When an expression mixes operators, precedence decides which runs first, just like in math (* before +). Use parentheses to make intent explicit and override the defaults.

<?php
echo 2 + 3 * 4;     // 14  — * binds tighter than +
echo (2 + 3) * 4;   // 20  — parentheses force addition first

From highest to lowest, a few key levels: **, then !, then * / %, then + -, then comparison (< > == ===), then &&, then ||, and finally =. When in doubt, add parentheses — they cost nothing and prevent bugs.

Summary

  • Arithmetic (+ - * / % **) operates on numbers; / returns a float, % returns the remainder.
  • Assignment (= += .= …) stores and updates values; compound forms are shorthand.
  • Comparison (== === != <=>) returns booleans — prefer strict === to avoid type-juggling surprises.
  • Logical (&& || !) combines conditions and short-circuits.
  • Increment/decrement (++ --) differ by pre/post position.
  • String (. .=) joins text; array (+ == ===) compares and merges arrays.
  • ?? supplies defaults for missing values; precedence and parentheses control evaluation order.

Next, put these operators to work inside conditional statements and loops.

Practice

Practice
Which of the following are valid types of operators in PHP?
Which of the following are valid types of operators in PHP?
Was this page helpful?