Operators
Learn PHP's arithmetic, assignment, comparison, logical, and string operators, including the critical difference between == and ===.
Introduction
Operators are the symbols PHP uses to perform actions on values — adding numbers, comparing them, combining strings, or checking logical conditions. You already saw a few in the last lesson while exploring type casting; this lesson covers the full set you will use in almost every PHP script you write.
- Arithmetic operators for doing math.
- Assignment operators, including shorthand forms like += and -=.
- Comparison operators, and the crucial difference between == and ===.
- The spaceship operator <=> for combined comparisons.
- Logical operators (&&, ||, !) and their word-based equivalents.
- String concatenation with . and .=
Arithmetic Operators
PHP supports the standard set of arithmetic operators, plus a dedicated exponentiation operator and a modulus (remainder) operator.
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 5 + 2 | 7 |
| - | Subtraction | 5 - 2 | 3 |
| * | Multiplication | 5 * 2 | 10 |
| / | Division | 5 / 2 | 2.5 |
| % | Modulus (remainder) | 5 % 2 | 1 |
| ** | Exponentiation | 5 ** 2 | 25 |
<?php
$a = 10;
$b = 3;
echo $a + $b; // 13
echo "\n";
echo $a - $b; // 7
echo "\n";
echo $a * $b; // 30
echo "\n";
echo $a / $b; // 3.3333333333333
echo "\n";
echo $a % $b; // 1
echo "\n";
echo $a ** $b; // 1000
?>13
7
30
3.3333333333333
1
1000Assignment Operators
The basic assignment operator = stores a value in a variable. PHP also offers compound assignment operators that combine an arithmetic operation with assignment in a single step.
| Operator | Equivalent To | Example |
|---|---|---|
| x = y | x = y | $x = 5; |
| x += y | x = x + y | $x += 3; |
| x -= y | x = x - y | $x -= 3; |
| x *= y | x = x * y | $x *= 3; |
| x /= y | x = x / y | $x /= 3; |
| x %= y | x = x % y | $x %= 3; |
| x **= y | x = x ** y | $x **= 2; |
<?php
$score = 10;
$score += 5; // $score is now 15
$score -= 2; // $score is now 13
$score *= 2; // $score is now 26
echo $score;
?>26Comparison Operators
Comparison operators check the relationship between two values and always produce a boolean result — true or false.
| Operator | Name | Description |
|---|---|---|
| == | Equal (loose) | True if values are equal after type conversion. |
| === | Identical (strict) | True if values are equal AND of the same type. |
| != or <> | Not equal (loose) | True if values are not equal after type conversion. |
| !== | Not identical | True if values differ in value or type. |
| > | Greater than | True if left is greater than right. |
| < | Less than | True if left is less than right. |
| >= | Greater than or equal | True if left is greater than or equal to right. |
| <= | Less than or equal | True if left is less than or equal to right. |
== vs === (Loose vs Strict)
This is one of the most important distinctions in all of PHP. The == operator ("loose equality") converts both sides to a common type before comparing, which can lead to surprising results. The === operator ("strict equality") requires both the value and the type to match exactly, with no conversion at all.
<?php
var_dump(0 == "abc"); // In PHP 8+: false (fixed from older PHP versions)
var_dump(0 == ""); // false
var_dump("1" == "01"); // true - both convert to the number 1
var_dump("10" == "1e1"); // true - both convert to the number 10
var_dump(100 == "1e2"); // true - both convert to the number 100
var_dump(0 == "0"); // true - both convert to the number 0
var_dump("1" === "01"); // false - different strings, no conversion
var_dump(100 === "1e2"); // false - int vs string, no conversion
var_dump(0 === "0"); // false - int vs string
var_dump(1 === 1.0); // false - int vs float, different types
?>bool(false)
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)Loose equality (==) has historically produced confusing results when comparing numeric strings, so most modern PHP style guides recommend defaulting to === (and !==) unless you have a specific, deliberate reason to allow type conversion.
The Spaceship Operator
The spaceship operator <=> compares two values and returns -1, 0, or 1 depending on whether the left side is less than, equal to, or greater than the right side. It is especially useful inside custom sorting functions.
<?php
echo 1 <=> 2; // -1 (1 is less than 2)
echo "\n";
echo 2 <=> 2; // 0 (equal)
echo "\n";
echo 3 <=> 2; // 1 (3 is greater than 2)
// A common use case: sorting an array of numbers
$numbers = [5, 3, 8, 1];
usort($numbers, fn($a, $b) => $a <=> $b);
print_r($numbers);
?>-1
0
1
Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 8
)Logical Operators
Logical operators combine or invert boolean expressions. PHP offers both symbol-based operators and word-based equivalents, which have subtly different precedence but behave the same in most everyday code.
| Symbol | Word Form | Meaning |
|---|---|---|
| && | and | True if both sides are true. |
| || | or | True if at least one side is true. |
| ! | (no word form) | Inverts a boolean: true becomes false and vice versa. |
| (none) | xor | True if exactly one side is true, but not both. |
<?php
$isLoggedIn = true;
$isAdmin = false;
var_dump($isLoggedIn && $isAdmin); // false - both must be true
var_dump($isLoggedIn || $isAdmin); // true - at least one is true
var_dump(!$isLoggedIn); // false - inverts true
var_dump($isLoggedIn xor $isAdmin); // true - exactly one is true
?>bool(false)
bool(true)
bool(false)
bool(true)The symbol forms (&&, ||) have higher precedence than the word forms (and, or). In practice this rarely matters, but it can cause subtle bugs when mixed with the = assignment operator, so most style guides recommend sticking to && and || consistently.
String Concatenation Operators
PHP uses the dot (.) operator to join strings together, and the .= operator to append a string onto an existing variable — the string equivalent of +=.
<?php
$first = "Hello";
$last = "World";
$greeting = $first . ", " . $last . "!";
echo $greeting; // "Hello, World!"
echo "\n";
$message = "Loading";
$message .= "...";
$message .= " done";
echo $message; // "Loading... done"
?>Hello, World!
Loading... doneOperator Precedence
When an expression mixes multiple operators, PHP evaluates them in a fixed order of precedence — similar to the order of operations in math class. Parentheses always override the default order, so use them freely to make your intent explicit.
<?php
echo 2 + 3 * 4; // 14 - multiplication happens before addition
echo "\n";
echo (2 + 3) * 4; // 20 - parentheses force addition first
?>14
20Common Mistakes
- Using == when you actually need === — this is the single most common source of subtle PHP bugs.
- Confusing the concatenation operator (.) with the addition operator (+) — PHP does not use + for strings.
- Forgetting that / can return a float even when both operands are integers, such as 5 / 2.
- Mixing the symbol and word forms of logical operators (&& vs and) without understanding their different precedence.
Best Practices
- Default to === and !== unless you have a specific reason to allow type conversion.
- Use parentheses to make precedence explicit in any non-trivial expression.
- Prefer && and || over and/or for consistency and to avoid precedence surprises.
- Reach for the spaceship operator (<=>) when writing custom sort comparisons.
Frequently Asked Questions
What is the actual difference between == and ===?
== ("loose equality") converts both values to a common type before comparing, while === ("strict equality") requires both the value and the type to already match, with no conversion performed.
Should I always use === instead of ==?
In most modern PHP code, yes. Strict comparison avoids the surprising type-conversion behavior of loose comparison and is generally considered the safer default.
What does the spaceship operator return exactly?
It returns an integer: -1 if the left operand is smaller, 0 if they are equal, and 1 if the left operand is larger — perfect for use inside functions like usort().
Why does 5 / 2 return 2.5 instead of 2?
PHP's division operator always performs real division and returns a float when the result is not a whole number. Use intdiv() if you specifically need integer division.
Key Takeaways
- Arithmetic operators include +, -, *, /, %, and ** for exponentiation.
- Assignment operators like += and -= combine an operation with assignment.
- == compares values loosely (with type conversion); === compares strictly (value and type).
- The spaceship operator <=> returns -1, 0, or 1 for use in comparisons and sorting.
- Logical operators && and || (or and/or) combine boolean expressions; ! inverts them.
- The . operator concatenates strings, and .= appends to an existing string variable.
Summary
Operators are the building blocks of every expression you will write in PHP. Of everything covered here, the == vs === distinction is the one worth committing to memory first, since it quietly causes more bugs than almost anything else in beginner PHP code.
- You can perform arithmetic, assignment, and comparison operations confidently.
- You understand the critical difference between == and ===.
- You know how to use the spaceship operator for comparisons and sorting.
- You can combine logical conditions with && and ||, and strings with . and .=.