An operator is a special character or mathematical symbol that tells the PHP compiler to perform specific operations on one, two, or more operands (variables or values). PHP provides a rich collection of operators ranging from traditional arithmetic to modern PHP 8 comparison tools like the Spaceship operator (<=>) and the Null Coalescing operator (??).
Used to perform basic mathematical operations on numeric values:
| Operator | Operation Name | Code Example | Result |
|---|---|---|---|
+ | Addition | $x = 10 + 5; | 15 |
- | Subtraction | $x = 10 - 5; | 5 |
* | Multiplication | $x = 10 * 5; | 50 |
/ | Division | $x = 10 / 4; | 2.5 |
% | Modulus (Remainder) | $x = 10 % 3; | 1 |
** | Exponentiation (Power) | $x = 2 ** 4; | 16 (2 to the 4th power) |
== vs ===Comparison operators evaluate relationships between two values and return a boolean (true or false):
| Operator | Name | Description | Example (with $a = 5, $b = "5") |
|---|---|---|---|
== |
Equal | True if values are equal after type juggling. | $a == $b → true |
=== |
Identical | True ONLY if values AND data types match strictly. | $a === $b → false |
!= or <> |
Not Equal | True if values are not equal. | $a != 10 → true |
!== |
Not Identical | True if values or data types are not identical. | $a !== $b → true |
>=, <= |
Greater/Less or Equal | Standard boundary comparisons. | $a >= 5 → true |
$a <=> $bThe three-way comparison spaceship operator returns -1, 0, or 1 in a single operation:
<?php
// $a <=> $b
echo 5 <=> 10; // Outputs: -1 (Left is smaller)
echo 10 <=> 10; // Outputs: 0 (Both are equal)
echo 15 <=> 10; // Outputs: 1 (Left is greater)
// Used extensively for clean custom array sorting:
$students = [
["name" => "Rahul", "marks" => 78],
["name" => "Sneha", "marks" => 92],
["name" => "Amit", "marks" => 85]
];
usort($students, fn($a, $b) => $b['marks'] <=> $a['marks']); // Sort Descending
?>
??Replaces bulky isset() checks when reading query parameters or form submissions:
<?php
// OLD verbose ternary way:
$page = isset($_GET['page']) ? $_GET['page'] : 1;
// MODERN clean PHP 8 way:
$page = $_GET['page'] ?? 1;
// Null coalescing assignment operator (??=)
$config['theme'] ??= 'dark'; // Sets 'dark' ONLY if not already set
?>
Used to combine multiple conditional expressions:
&& or and (Logical AND): True if both conditions are true: if ($age >= 18 && $hasLicense)|| or or (Logical OR): True if at least one condition is true: if ($isCash || $isCard)! (Logical NOT): Inverts truth value: if (!$isLoggedIn)xor (Exclusive OR): True if either condition is true, but not both.High-Frequency Exam Question:
Q: Distinguish between Pre-Increment (++$x) and Post-Increment ($x++).
A: ++$x (pre-increment) increments $x by 1 first, then returns the new value. $x++ (post-increment) returns the original value of $x first, and then increments it afterwards.
Exercise 10.1: Write a script that checks if a user is eligible for an exam: must have attendance >= 75 AND (feesPaid == true OR scholarshipHolder == true).
Exercise 10.2: Sort an array of product prices [499, 120, 1500, 89] in ascending order using usort() and the spaceship operator.