Operators perform operations on variables and values.
Compare two values and return a boolean result.
| Operator | Name | Example | Result |
|---|
== | Equal | 5 == "5" | true (type coercion) |
=== | Identical | 5 === "5" | false (strict) |
!= | Not equal | 5 != 3 | true |
!== | Not identical | 5 !== "5" | true (strict) |
> | Greater than | 10 > 5 | true |
< | Less than | 3 < 7 | true |
>= | Greater or equal | 5 >= 5 | true |
<= | Less or equal | 4 <= 3 | false |
$a == $b // true (values equal, types ignored)
$a === $b // false (different types)
// Common type coercion cases
Combine multiple conditions.
| Operator | Name | Description |
|---|
&& or and | AND | Both conditions must be true |
|| or or | OR | At least one must be true |
! | NOT | Reverses the condition |
xor | XOR | Exactly one must be true |
// AND - both must be true
if ($age >= 21 && $income >= 30000) {
// OR - at least one must be true
if ($age >= 65 || $income < 20000) {
echo "Eligible for discount";
// NOT - reverses boolean
// XOR - exactly one must be true
if ($useCreditCard xor $usePayPal) {
echo "Payment method selected";
Operator precedence: ! → && → ||. Use parentheses for clarity.
| Operator | Name | Example | Result |
|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 4 * 3 | 12 |
/ | Division | 15 / 3 | 5 |
% | Modulus | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
$total = $price * $quantity - $discount;
$isEven = ($number % 2 == 0);
| Operator | Example | Equivalent |
|---|
= | $a = 5 | Assign |
+= | $a += 3 | $a = $a + 3 |
-= | $a -= 2 | $a = $a - 2 |
*= | $a *= 4 | $a = $a * 4 |
/= | $a /= 2 | $a = $a / 2 |
.= | $a .= "x" | $a = $a . "x" |
$message .= "!"; // "Hi!"
| Operator | Description |
|---|
++$a | Pre-increment (increment, then return) |
$a++ | Post-increment (return, then increment) |
--$a | Pre-decrement |
$a-- | Post-decrement |
echo $count++; // 5, then becomes 6
echo ++$count; // 7, then outputs 7
| Operator | Name | Example |
|---|
. | Concatenation | "Hi" . " there" |
.= | Append | $a .= " more" |
$name = $firstName . " " . $lastName;
$html .= "<p>Content</p>";
- Use strict comparison (
===) unless you need type coercion.
- Use parentheses when combining multiple logical operators for clarity.
- Use compound operators (
+=, -=, .=) for cleaner code.
- Be aware of type coercion with
== vs ===.