Operators in PHP (All Types with Output)
In this lesson: PHP operators with examples and output — arithmetic, assignment, comparison (== vs ===), logical, string and spaceship.
What is an operator
An operator is a symbol that performs an action on values — like + to add, > to compare, or . to join text. The values an operator works on are called operands. PHP groups operators into arithmetic, assignment, comparison, logical and string operators.
$total = 5 + 3, the + and = are operators, while 5 and 3 are operands.Arithmetic operators
These do maths. The one beginners forget is % (remainder).
<?php
echo 10 + 3; // 13
echo 10 - 3; // 7
echo 10 * 3; // 30
echo 10 / 3; // 3.333...
echo 10 % 3; // 1 (remainder)
?>
To join text, PHP uses the dot: "Hello " . "World" gives "Hello World". Do not use + for strings in PHP.
Assignment operators
= stores a value. The shortcuts combine maths with assignment.
<?php
$x = 10;
$x += 5; // same as $x = $x + 5 -> 15
$x -= 3; // 12
$x *= 2; // 24
echo $x;
?>
These shortcuts (+=, -=, *=, /=) make code shorter and are used a lot inside loops.
Comparison operators
| Operator | Meaning | Example (true?) |
|---|---|---|
| > | greater than | 10 > 3 → true |
| < | less than | 3 < 10 → true |
| >= | greater or equal | 5 >= 5 → true |
| != | not equal | 5 != 3 → true |
| == | equal (value) | "5" == 5 → true |
| === | equal (value + type) | "5" === 5 → false |
Comparison operators return true or false, which is exactly what conditions need.
Logical operators
These combine true/false conditions.
<?php
$age = 20;
$hasID = true;
if ($age >= 18 && $hasID) {
echo "Allowed";
}
?>
&& (AND) needs both true; || (OR) needs at least one true; ! (NOT) flips true to false.
== vs === (important)
This is the most important operator detail in PHP. == checks value only; === checks value and type.
<?php
var_dump("5" == 5); // true (types ignored)
var_dump("5" === 5); // false (string vs int)
?>
bool(false)
Because == can give surprising results with mixed types, prefer === whenever you can, especially when comparing form input.
Common mistakes
- Using
+to join strings. PHP joins with.;+tries to add numbers. - Using
==when you need===. Loose comparison can make "0" or "" behave unexpectedly. - Confusing
=and==. One assigns, the other compares. - Forgetting operator precedence. Use brackets to make the order clear:
($a + $b) * $c.
Summary
- Operators act on values: arithmetic, assignment, comparison, logical, string.
- Join strings with
.and get remainder with%. - Comparisons return true/false for use in conditions.
&&,||,!combine conditions.- Prefer
===(value + type) over==to avoid surprises.
Next, use these operators inside if-else decisions, or repeat with loops in PHP.
Frequently Asked Questions
What is the difference between == and === in PHP?
== compares only the values and ignores type, so "5" == 5 is true. === compares value AND type, so "5" === 5 is false because one is text and the other is a number. Prefer === to avoid surprises.How do I join two strings in PHP?
. — for example "Hello " . "World" gives "Hello World". PHP uses a dot for joining strings, not the plus sign like some other languages.What does the % operator do?
% gives the remainder after division. 10 % 3 is 1. It is very useful to check even/odd ($n % 2 == 0 means even) or to repeat things in cycles.What is the difference between ++ before and after a variable?
$i++ (post) uses the current value first, then adds 1. ++$i (pre) adds 1 first, then uses it. The final value of $i is the same; only the value used in that expression differs.What do && and || mean?
&& is AND — true only if both sides are true. || is OR — true if at least one side is true. They combine conditions, like $age >= 18 && $hasID.