Chapter 7: Operators
An operator is a symbol that does something with values: add them, compare
them, join them. You have already used the most important one, =, which puts a
value into a variable. This chapter covers the rest.
Arithmetic
$sum = 10 + 5; // 15
$difference = 10 - 5; // 5
$product = 10 * 5; // 50
$quotient = 10 / 4; // 2.5 — division always yields a float
$remainder = 10 % 3; // 1 — the modulo (rest after dividing)
$power = 2 ** 10; // 1024 — exponent
Modulo is the remainder of a division: 10 % 3 is 1 because
10 divided by 3 is 3 with 1 left over. It is how you test for even and odd:
$n % 2 == 0 means $n is even.
10 / 4 is 2.5 and 10 / 2 is
5.0 — division always produces a float, never an integer.
Use intdiv() when you want a whole number back.
Assignment operators
= stores a value. The combined operators do a calculation and store the
result in the same step:
$score = 100;
$score += 25; // 125 — same as $score = $score + 25
$score -= 50; // 75
$score *= 2; // 150
$score /= 5; // 30
$score %= 7; // 2 — the rest of 30 / 7
Watch the types while you do this: $score /= 5 keeps $score an
integer, so it rounds down if the result has decimals. The plain operator behaves
differently — $score / 4 is a float no matter what was on the left.
There is also .=, which appends to a string:
$name = "Ada";
$name .= " Lovelace";
echo $name; // Ada Lovelace
.= needs a string on the left, so appending to
$n = 9; is a compile error — the checker tells you before the program ever runs.
Increment and decrement
$i = 5;
$i++; // $i is now 6
$i--; // $i is back to 5
echo $i;
These are written as their own statement: $i++; on a line by itself.
When you need the value in the middle of an expression, do the arithmetic there
instead ($i + 1).
Comparisons
| Operator | Reads as |
|---|---|
== | equal to (loose: types may differ) |
!= | not equal to (loose) |
=== | identical: same value and same type |
!== | not identical |
< > <= >= | smaller, greater, smaller-or-equal, greater-or-equal |
<=> | spaceship: -1, 0 or 1 |
echo ("5" == 5 ? "loose" : "strict"); // loose
echo ("5" === 5 ? "same" : "different"); // different
echo (3 <=> 5); // -1
echo (5 <=> 5); // 0
===: == converts types to compare them, which is
convenient ("5" == 5) and surprising (0 == ""). Use
=== unless you specifically want PHP's loose behaviour.
Logical operators
$age = 20;
$hasTicket = true;
if ($age >= 18 && $hasTicket) {
echo "you may come in";
}
if ($age < 0 || $age > 120) {
echo "that age looks wrong";
}
if (!$hasTicket) {
echo "no ticket";
}
&& needs both sides to be true, || needs one, and
! flips a value. Both && and || stop as soon
as the answer is decided — if the left side of && is false, the right
side is never evaluated.
Ternary and null coalescing
$age = 20;
$msg = $age >= 18 ? "adult" : "minor";
echo $msg; // adult
$config = null;
$mode = $config ?? "default";
echo $mode; // default
condition ? a : b picks one of two values. ?? returns the value
on the right only when the left side is null (or missing), which is how you
give optional settings a fallback.
Bitwise (a glimpse)
echo (5 & 3); // 1 — bits set in both
echo (5 | 3); // 7 — bits set in either
echo (5 ^ 3); // 6 — bits set in one but not the other
echo (1 << 4); // 16 — shift left: 1 moved four places
echo (12 >> 2); // 3 — shift right
These work on the binary representation of integers. Everyday programs rarely need them, but they show up in flags, masks and packing things into small numbers.
Precedence: who binds tighter
When an expression mixes operators, the tighter ones grab their operands first:
| Level | Operators |
|---|---|
| 1 (tightest) | ** |
| 2 | * / % |
| 3 | + - |
| 4 | . (concatenation) |
| 5 | comparisons: == === < > <=> … |
| 6 | && |
| 7 | || |
| 8 | ? : |
| 9 (loosest) | = += -= .= … |
So 1 + 2 . "x" is "3x" — the addition happens first, then the two
pieces are joined. When in doubt, add parentheses: they cost nothing and read better than a
precedence puzzle.
%, doubles it with *, prints a sentence built with
., and uses ? : to pick the sentence's wording.