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.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 4 | 2.5 |
% | Modulo (remainder) | 10 % 3 | 1 |
** | Exponentiation | 2 ** 4 | 16 |
<?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 / 4is2.5, not2. Useintdiv()for integer division. The%operator works on integers; for floating-point remainders usefmod().
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.
| Operator | Same 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; // 140Comparison Operators
Comparison operators compare two values and return a boolean (true or false). Notice the important distinction between loose (==) and strict (===) comparison.
| Operator | Name | true when... |
|---|---|---|
== | Equal | values are equal after type juggling |
=== | Identical | values and types are equal |
!= / <> | Not equal | values are not equal |
!== | Not identical | values or types differ |
< | Less than | left is smaller |
> | Greater than | left 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"isfalsein 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.
| Operator | Name | true when... |
|---|---|---|
&& / and | And | both sides are true |
|| / or | Or | at least one side is true |
! | Not | the operand is false |
xor | Exclusive or | exactly 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)
&&andandbehave the same in conditions but have different precedence —and/orbind 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.
| Operator | Effect |
|---|---|
++$x | Pre-increment: increment, then return the new value |
$x++ | Post-increment: return the old value, then increment |
--$x | Pre-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 rocksArray Operators
Array operators work on whole arrays.
| Operator | Name | Description |
|---|---|---|
+ | Union | keys from the right are added only if missing on the left |
== | Equality | same key/value pairs |
=== | Identity | same 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 addedNull-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 / MemberThe ??= 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 firstFrom 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.