Operators combine or compare values. Most will look familiar from other languages — a few of PHP's own quirks are worth flagging specifically.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 5 + 2 → 7 |
| - | Subtraction | 5 - 2 → 3 |
| * | Multiplication | 5 * 2 → 10 |
| / | Division | 5 / 2 → 2.5 |
| % | Modulus (remainder) | 5 % 2 → 1 |
| ** | Exponent | 5 ** 2 → 25 |
PHP uses a dot (.) to join strings together — not +, which is reserved purely for arithmetic:
<?php
$first = "Learn";
$second = "Computer";
echo $first . " " . $second; // "Learn Computer"
$greeting = "Hello, ";
$greeting .= "world!"; // .= appends, same idea as +=
echo $greeting;
?>This is the single most important operator distinction in PHP. == compares values only, allowing type juggling first; === compares both value and type, with no conversion at all.
<?php
var_dump(0 == "abc"); // false in modern PHP (was true pre-8.0 — a famous gotcha)
var_dump(0 == "0"); // true — "0" converts to 0
var_dump(0 === "0"); // false — different types, no conversion
var_dump("5" == 5); // true
var_dump("5" === 5); // false
?>Prefer === by default
Default to === (and !== for "not equal") unless you have a specific reason to want type juggling. It removes an entire category of subtle bugs before they can happen.
| Operator | Meaning |
|---|---|
| && (or and) | True only if both sides are true |
| || (or or) | True if either side is true |
| ! | Flips true to false and vice versa |
<?php
$age = 20;
$hasId = true;
if ($age >= 18 && $hasId) {
echo "Entry allowed.";
}
?>