Operators are used to perform operations on variables and values. In PHP, operators are grouped into several categories based on the type of operation they perform—from simple math to complex logical evaluations.
To help you learn, PHP operators are divided into the following categories:
Used with numeric values to perform common mathematical operations, like addition or subtraction.
Used with numeric values to write a value to a variable (e.g., = or +=).
Used to compare two values (number or string) and return a boolean result.
Used to combine conditional statements (e.g., and, or, not).
Most operators work with two values (operands). For example, in the expression 5 + 10, 5 and 10 are operands, and + is the operator.
<?php
$x = 10;
$y = 4;
echo $x + $y; // Math operator
echo ($x == $y); // Comparison operator
?>
Beyond math and logic, PHP includes several specialized operators that make your code more concise:
. and .=).? :) and the Null Coalescing operator (??).?? (Null Coalescing) are powerful modern features of PHP.+ to join strings. Coming from JavaScript, it's natural to reach for + — but in PHP, + is strictly numeric addition. Use the dot operator . to concatenate strings instead.
= instead of == in a condition. if ($ready = true) assigns true to $ready and the condition always passes — it doesn't compare anything.
== for important comparisons. Loose equality performs type juggling that can produce surprising results with mixed types. Prefer === unless you specifically want type coercion.
This is the single most common operator mistake for developers switching from JavaScript to PHP:
<?php
$first = "Hello";
$second = "World";
echo $first + $second; // 0 (both treated as numbers, both are 0)
echo $first . " " . $second; // Hello World (correct concatenation)
?>
Q: Does + concatenate strings in PHP?
A: No — + is always numeric addition in PHP. Use the dot operator . (or .= to append) for string concatenation.
Q: What's the difference between == and ===?
A: == compares values after converting types if needed (loose); === requires both the value and the type to match (strict). Default to === unless you have a specific reason not to.
Q: What does the ?? operator do?
A: The null coalescing operator — $name = $input ?? "Guest"; uses $input if it's set and not null, otherwise falls back to "Guest". See the PHP manual's operators reference for the full list.