PHP Operators
Operators are symbols that tell PHP to perform specific operations on values or variables. PHP supports a wide range of operators for arithmetic, comparison, logical decisions, string handling, and more. Understanding operators is essential because they form the backbone of any expression or condition in a PHP script.
Arithmetic Operators
Arithmetic operators perform standard mathematical calculations.
<?php
$a = 20;
$b = 6;
echo $a + $b; // Addition - Outputs: 26
echo $a - $b; // Subtraction - Outputs: 14
echo $a * $b; // Multiplication - Outputs: 120
echo $a / $b; // Division - Outputs: 3.333...
echo $a % $b; // Modulus - Outputs: 2 (remainder of 20 / 6)
echo $a ** $b; // Exponentiation - Outputs: 64000000 (20 to the power of 6)
?>
The modulus operator (%) returns the remainder after division. It is commonly used to check whether a number is even or odd.
Assignment Operators
Assignment operators assign a value to a variable. The basic assignment operator is =. PHP also provides shorthand combined assignment operators.
<?php
$score = 100;
$score += 10; // Same as: $score = $score + 10 - Result: 110
$score -= 5; // Same as: $score = $score - 5 - Result: 105
$score *= 2; // Same as: $score = $score * 2 - Result: 210
$score /= 3; // Same as: $score = $score / 3 - Result: 70
$score %= 8; // Same as: $score = $score % 8 - Result: 6
echo $score; // Outputs: 6
?>
String Assignment Shorthand
<?php
$message = "Hello";
$message .= " World"; // Appends to existing string
echo $message; // Outputs: Hello World
?>
Comparison Operators
Comparison operators compare two values and return a boolean result — true or false. They are primarily used inside conditional statements.
<?php
$x = 10;
$y = "10";
var_dump($x == $y); // true - equal in value (loose comparison)
var_dump($x === $y); // false - not identical (different types)
var_dump($x != $y); // false - not different in value
var_dump($x !== $y); // true - not identical (type difference exists)
var_dump($x > 5); // true
var_dump($x < 5); // false
var_dump($x >= 10); // true
var_dump($x <= 9); // false
?>
Equal vs Identical
This distinction matters: == checks only value, while === checks both value and type.
<?php
$num = 0;
$empty = "";
$nullVal = null;
var_dump($num == $empty); // true - both are "falsy" in loose comparison
var_dump($num === $empty); // false - different types (integer vs string)
var_dump($num == $nullVal); // true - loose comparison
var_dump($num === $nullVal); // false - different types
?>
Use === whenever possible to avoid unexpected comparisons caused by PHP's automatic type conversion.
Increment and Decrement Operators
These operators increase or decrease a variable's value by one.
<?php
$counter = 5;
$counter++; // Post-increment: uses value first, then increments
echo $counter; // Outputs: 6
++$counter; // Pre-increment: increments first, then uses value
echo $counter; // Outputs: 7
$counter--; // Post-decrement
echo $counter; // Outputs: 6
--$counter; // Pre-decrement
echo $counter; // Outputs: 5
?>
Logical Operators
Logical operators combine multiple conditions and return a boolean result.
<?php
$age = 25;
$hasID = true;
// AND - both conditions must be true
if ($age >= 18 && $hasID) {
echo "Entry allowed.";
}
// OR - at least one condition must be true
$isStudent = false;
$isStaff = true;
if ($isStudent || $isStaff) {
echo "Access granted.";
}
// NOT - reverses the boolean
$isBanned = false;
if (!$isBanned) {
echo "User is not banned.";
}
?>
| Operator | Symbol | Alternative | Description |
|---|---|---|---|
| AND | && | and | True if both sides are true |
| OR | || | or | True if at least one side is true |
| NOT | ! | not | Reverses the boolean value |
| XOR | xor | — | True if only one side is true |
String Operators
PHP has two operators specifically for strings.
<?php
$firstName = "John";
$lastName = "Doe";
// Concatenation operator - joins two strings
$fullName = $firstName . " " . $lastName;
echo $fullName; // Outputs: John Doe
// Concatenation assignment - appends to existing string
$sentence = "PHP is ";
$sentence .= "powerful.";
echo $sentence; // Outputs: PHP is powerful.
?>
Spaceship Operator
Introduced in PHP 7, the spaceship operator (<=>) compares two values and returns -1, 0, or 1 depending on which side is larger. It is useful for custom sorting.
<?php
echo (1 <=> 2); // Outputs: -1 (left is less)
echo (2 <=> 2); // Outputs: 0 (both are equal)
echo (3 <=> 2); // Outputs: 1 (left is greater)
?>
Null Coalescing Operator
The null coalescing operator (??) returns the left side if it is set and not null; otherwise, it returns the right side. It replaces a common pattern of checking for null before using a variable.
<?php
$username = $_GET['user'] ?? "Guest";
echo $username; // Outputs: Guest if $_GET['user'] is not set
?>
Key Points
- Arithmetic operators perform math:
+,-,*,/,%,**. - Assignment operators set values:
=,+=,-=,*=, etc. - Use
===instead of==for strict type-safe comparisons. - Logical operators (
&&,||,!) combine or negate conditions. - The dot (
.) operator joins strings. - The spaceship operator (
<=>) returns -1, 0, or 1 for three-way comparisons. - The null coalescing operator (
??) provides a safe fallback for potentially null values.
