Operators Overview
An operator is a symbol (or a short keyword) that tells PHP to perform an action on one or more values, called operands. $a + $b adds two numbers, $a . $b glues two strings together, and $a === $b asks whether two values are identical. You have already used several operators without naming them — the = in every variable assignment is one. This page is a map of every operator family in PHP so that the dedicated pages that follow (arithmetic, assignment, comparison, logical, string, null coalescing, and precedence) build on a shared vocabulary.
Arithmetic operators
These perform ordinary math: addition, subtraction, multiplication, division, modulo (remainder), and exponentiation. PHP also has increment (++) and decrement (--) shorthand for adding or subtracting one.
Arithmetic in one glance
<?php $sum = 4 + 2; // 6 $diff = 4 - 2; // 2 $product = 4 * 2; // 8 $quotient = 4 / 2; // 2 $remainder = 5 % 2; // 1 $power = 2 ** 8; // 256
Assignment operators
The plain = stores a value in a variable. Compound assignment operators combine an arithmetic or string operation with assignment in one step, so $total += 5 means the same thing as $total = $total + 5, just shorter.
Compound assignment
<?php $total = 10; $total += 5; // 15 $total -= 3; // 12 $total *= 2; // 24 $message = "Hi"; $message .= " there"; // "Hi there"
Comparison operators
Comparison operators check the relationship between two values and always produce a boolean result: true or false. PHP has both loose comparison (==, checks value only, allowing type conversion) and strict comparison (===, checks value and type). It also has ordering operators such as <, >, <=, and >=, plus the three-way spaceship operator <=>.
Loose vs strict
<?php var_dump(1 == "1"); // bool(true) - loose, types converted var_dump(1 === "1"); // bool(false) - strict, int vs string var_dump(5 <=> 3); // int(1) - left is greater
Logical operators
Logical operators combine or invert boolean expressions: && (and), || (or), and ! (not) are the everyday forms, alongside the word-based and, or, and xor, which read naturally but have surprisingly low precedence — a detail the Logical Operators page covers with a real example of a bug it causes.
Combining conditions
<?php
$age = 25;
$hasLicense = true;
if ($age >= 18 && $hasLicense) {
echo "Can drive.";
}String operators
PHP has exactly two string-specific operators: the concatenation operator . glues two strings into one, and .= appends to an existing string variable. Unlike JavaScript, PHP's + is never used for joining strings — it always performs numeric addition.
Concatenation
<?php $first = "Let"; $second = "Codes"; $brand = $first . " " . $second; // "Let Codes"
Array operators
Arrays support union (+), and the equality family: == checks that both arrays have the same key/value pairs regardless of order, while === additionally requires the same order and the same types.
Array union
<?php $defaults = ["color" => "blue", "size" => "M"]; $custom = ["size" => "L"]; $merged = $custom + $defaults; print_r($merged); // color => blue, size => L (custom wins on overlap)
Incrementing and decrementing
++$x (pre-increment) increases a value before it is used in an expression; $x++ (post-increment) uses the current value first and increases it afterward. The same pre/post distinction applies to --. The Arithmetic Operators page walks through this with output you can trace line by line.
Type operators
The instanceof operator checks whether an object is an instance of a particular class or implements a particular interface — it is covered in depth once classes are introduced, but it is worth knowing it exists in this family of operators.
Null coalescing and spaceship
Two newer additions round out the picture: the null coalescing operator ?? returns its left operand if it exists and is not null, otherwise it returns the right operand — perfect for default values. The spaceship operator <=> returns -1, 0, or 1 depending on whether the left side is less than, equal to, or greater than the right side, which is exactly the contract usort() expects from a comparison callback.
Quick reference
Category | Examples | Typical use |
|---|---|---|
Arithmetic |
| Math on numbers |
Assignment | = += -= .= ??= | Storing and updating values |
Comparison | == === != !== < > <= >= <=> | Boolean checks and ordering |
Logical | && || ! and or xor | Combining conditions |
String | . .= | Building text |
Array |
| Merging and comparing arrays |
Increment/decrement | ++ -- | Counters and loops |
Type | instanceof | Checking object types |
Null coalescing | ?? ??= | Fallback values |