PHP — Operators
Arithmetic operators
Standard mathematical operations:
<?php
$a = 10;
$b = 3;
echo $a + $b; // 13 — Addition
echo $a - $b; // 7 — Subtraction
echo $a * $b; // 30 — Multiplication
echo $a / $b; // 3.3333 — Division
echo $a % $b; // 1 — Modulus (remainder)
echo $a ** $b; // 1000 — Exponentiation (PHP 5.6+)
?>
Division by zero throws a DivisionByZeroError in PHP 8+:
<?php
echo 10 / 0; // DivisionByZeroError
?>
Assignment operators
Assign and modify values:
<?php
$x = 10; // Basic assignment
$x += 5; // $x = $x + 5 → 15
$x -= 3; // $x = $x - 3 → 12
$x *= 2; // $x = $x * 2 → 24
$x /= 4; // $x = $x / 4 → 6
$x %= 4; // $x = $x % 4 → 2
$x **= 3; // $x = $x ** 3 → 8
?>
String concatenation assignment:
<?php
$log = "";
$log .= "Step 1: Initialize\n";
$log .= "Step 2: Connect\n";
echo $log;
?>
Comparison operators
Compare values and return booleans:
<?php
$a = 10;
$b = "10";
// Loose comparison (type coercion)
echo $a == $b; // true — "10" becomes 10
echo $a != $b; // false
echo $a <> $b; // false — same as !=
echo $a === $b; // false — different types
echo $a !== $b; // true — different types
// spaceship operator (PHP 7+)
echo 1 <=> 2; // -1 (left is less)
echo 2 <=> 2; // 0 (equal)
echo 3 <=> 2; // 1 (left is greater)
?>
Loose vs strict comparison
<?php
// Loose: converts types to match
0 == "foo"; // true ("foo" becomes 0)
"" == null; // true
"1" == "01"; // true
// Strict: no type conversion
0 === "foo"; // false
"" === null; // false
"1" === "01"; // false
// Always prefer === and !== to avoid surprises
?>
Logical operators
Combine boolean expressions:
<?php
$a = true;
$b = false;
echo $a && $b; // false — AND
echo $a || $b; // true — OR
echo !$a; // false — NOT
echo $a and $b; // false — AND (lower precedence)
echo $a or $b; // true — OR (lower precedence)
echo !$a; // false — NOT
?>
Warning: && and and have different precedence:
<?php
// Parentheses matter
$true && false or true; // true (evaluated as ($true && false) or true)
$true and false or true; // false (evaluated as $true and (false or true))
?>
Ternary operator
Compact if-else expressions:
<?php
$age = 20;
$status = ($age >= 18) ? "Adult" : "Minor";
echo $status; // Adult
// Nested ternary (avoid nesting — use match instead)
$score = 85;
$grade = ($score >= 90) ? "A" : (($score >= 80) ? "B" : "C");
echo $grade; // B
?>
Null coalescing operator
Safely provide defaults for null values:
<?php
$name = $_GET["name"] ?? "Guest";
// Equivalent to:
$name = isset($_GET["name"]) ? $_GET["name"] : "Guest";
// Works with nested values
$city = $user["address"]["city"] ?? "Unknown";
?>
Null coalescing assignment
Set a variable only if it's currently null:
<?php
$config = [];
$config["timeout"] ??= 30; // Set to 30 if not set
$config["retries"] ??= 3; // Set to 3 if not set
print_r($config);
// Array ( [timeout] => 30 [retries] => 3 )
?>
Spaceship operator
Returns -1, 0, or 1 for ordering comparisons:
<?php
echo 5 <=> 3; // 1
echo 3 <=> 5; // -1
echo 5 <=> 5; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
// Useful in usort callbacks
usort($array, fn($a, $b) => $a['age'] <=> $b['age']);
?>
Bitwise operators
Work on binary representations:
<?php
$a = 0b1010; // 10 in decimal
$b = 0b1100; // 12 in decimal
echo decbin($a & $b); // 1000 (8) — AND
echo decbin($a | $b); // 1110 (14) — OR
echo decbin($a ^ $b); // 0110 (6) — XOR
echo decbin(~$a); // ...11110101 — NOT
echo decbin($a << 1); // 10100 (20) — Left shift
echo decbin($a >> 1); // 101 (5) — Right shift
?>
Operator precedence
From highest to lowest (most common):
| Precedence | Operator | Description |
|---|---|---|
| 1 | () | Parentheses |
| 2 | ** | Exponentiation |
| 3 | ~ ++ -- | Unary, increment, decrement |
| 4 | * / % | Multiplication, division, modulus |
| 5 | + - | Addition, subtraction |
| 6 | . | String concatenation |
| 7 | < <= > >= <=> | Comparison |
| 8 | == != === !== <> | Equality |
| 9 | && and | Logical AND |
| 10 | || or | Logical OR |
Always use parentheses when precedence is unclear:
<?php
// Ambiguous — avoid this
$result = $a + $b * $c;
// Clear — prefer this
$result = $a + ($b * $c);
?>
Mini Practice
Write PHP code that:
- Demonstrates all five arithmetic operators
- Shows the difference between
==and=== - Uses the null coalescing operator to provide defaults for missing values
- Uses the spaceship operator to compare two values
Up Next
In the next lesson, you'll learn about If Else — conditional branching in PHP.
Related Topics
Frequently Asked Questions about Operators
What is Operators in PHP?
Operators is a fundamental concept in PHP. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Operators?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Operators.
Why is Operators important in PHP?
Operators is essential for PHP development. Understanding this concept will help you write better code and solve real-world problems more effectively.