In Part 2 we learned how to store data in variables. That's useful, but the real fun starts when we do something with that data. That's what operators are for — they let you calculate, compare, and combine values.
Let me walk you through the ones you'll use every day.
Arithmetic Operators
These are the math operators you already know, plus one you might not:
<?php
echo 10 + 3; // 13 (addition)
echo 10 - 3; // 7 (subtraction)
echo 10 * 3; // 30 (multiplication)
echo 10 / 3; // 3.333... (division)
echo 10 % 3; // 1 (modulus — the remainder)
echo 10 ** 3; // 1000 (exponent — 10 to the power of 3)
?>
The modulus (%) is the one beginners forget — it gives you the remainder of a division. It's surprisingly handy, for example to check if a number is even: $n % 2 is 0 for even numbers.
Assignment Operators
You already met the basic one in Part 2 — the equals sign (=). But there are handy shortcuts that combine math with assignment:
<?php
$x = 10;
$x += 5; // same as $x = $x + 5; → 15
$x -= 3; // → 12
$x *= 2; // → 24
$x .= "!"; // .= joins text → "24!"
?>
That last one, .=, appends text — we'll come back to it under string operators.
Comparison Operators
These compare two values and give back a boolean (true or false). This is where you'll spend a lot of time once we reach conditions:
<?php
var_dump(5 == 5); // true — equal value
var_dump(5 == "5"); // true — equal value (type ignored!)
var_dump(5 === "5"); // false — equal value AND type
var_dump(5 != 3); // true — not equal
var_dump(5 > 3); // true
var_dump(5 <= 5); // true
?>
Important beginner lesson: notice the difference between == and ===.
==checks if the values are equal, but ignores the type — so the number5and the text"5"are considered equal.===checks that the values and their types match.
As a rule of thumb, prefer === — it's stricter and saves you from sneaky bugs.
Increment & Decrement
Shortcuts for adding or subtracting 1 — you'll see these constantly in loops:
<?php
$count = 5;
$count++; // now 6 (increment)
$count--; // back to 5 (decrement)
?>
Logical Operators
These let you combine several true/false conditions:
<?php
$age = 25;
var_dump($age > 18 && $age < 65); // true — AND: both must be true
var_dump($age < 18 || $age > 65); // false — OR: at least one true
var_dump(!($age > 18)); // false — NOT: flips it
?>
&&(AND) — true only if both sides are true.||(OR) — true if either side is true.!(NOT) — flips true to false and vice versa.
String Operator
We saw this in Part 1: the dot (.) joins (concatenates) text together:
<?php
$first = "Coders";
$last = "Republic";
echo $first . " " . $last; // outputs: Coders Republic
?>
A quick note on order
Just like in math, operators have an order of precedence — * and / run before + and -, for example. When in doubt, use parentheses to make your intent clear and your code easier to read:
<?php
echo 2 + 3 * 4; // 14 (multiplication first)
echo (2 + 3) * 4; // 20 (parentheses first)
?>
To Be Continued…
Now that you can compare and combine values, you're ready to make your program make decisions. In Part 4 we'll cover Conditional Statements — if, else, and friends. Stay tuned!