Operators in PHP
What Are Operators in PHP?
Operators are symbols that tell PHP to perform a specific operation on one or more values, called operands. You've already been using them without necessarily naming them — the = sign that assigns a value to a variable, or the . that joins two strings together, are both operators. Understanding the full range of PHP's operators is essential because they form the building blocks of nearly every line of logic you'll write, from simple calculations to complex conditions.
PHP groups operators into several categories based on what they do. This lesson walks through each category with practical examples.
Arithmetic Operators
Arithmetic operators perform mathematical calculations, similar to what you'd expect from basic math.
php
<?php $a = 10; $b = 3; echo $a + $b; // 13 (addition) echo $a - $b; // 7 (subtraction) echo $a * $b; // 30 (multiplication) echo $a / $b; // 3.333... (division) echo $a % $b; // 1 (modulus - remainder after division) echo $a ** $b; // 1000 (exponentiation - 10 to the power of 3)
The modulus operator (%) is especially useful in real applications — it's commonly used to check whether a number is even or odd, or to create repeating patterns (like alternating row colors in a table).
Assignment Operators
Assignment operators store values in variables. The basic = operator is the most familiar, but PHP also offers combined assignment operators that update a variable based on its current value in a single step.
php
<?php $total = 10; $total += 5; // same as $total = $total + 5; → 15 $total -= 3; // same as $total = $total - 3; → 12 $total *= 2; // same as $total = $total * 2; → 24 $total /= 4; // same as $total = $total / 4; → 6 $total .= " items"; // string concatenation assignment → "6 items"
These combined operators are widely used in real code because they're shorter and clearer than repeating the full variable name on both sides of the equals sign.
Comparison Operators
Comparison operators compare two values and return a boolean (true or false). These are the backbone of nearly every conditional statement.
php
<?php $x = "5"; $y = 5; var_dump($x == $y); // true - loose comparison, checks value only var_dump($x === $y); // false - strict comparison, checks value AND type var_dump($x != $y); // false - not equal (loose) var_dump($x !== $y); // true - not equal (strict) var_dump(10 > 5); // true var_dump(10 <= 5); // false
The distinction between == and === is one of the most important concepts in PHP. Loose comparison (==) allows type juggling — PHP converts types before comparing, which can lead to unexpected results. Strict comparison (===) checks both value and data type, making it the safer default in most modern PHP code.
Logical Operators
Logical operators combine multiple conditions into a single true/false result, commonly used inside if statements.
php
<?php
$age = 25;
$hasLicense = true;
// AND - both conditions must be true
if ($age >= 18 && $hasLicense) {
echo "Allowed to drive.";
}
// OR - at least one condition must be true
if ($age < 18 || !$hasLicense) {
echo "Not allowed to drive.";
}
// NOT - reverses a boolean value
$isMinor = !($age >= 18);
PHP also supports word-based versions of these operators (and, or, xor), but they have lower precedence than && and ||, which can cause subtle bugs in certain expressions. Most PHP style guides recommend sticking with the symbol-based versions (&&, ||) for consistency and predictability.
String Operators
PHP uses a dedicated operator for joining strings together, separate from arithmetic addition.
php
<?php $firstName = "Rohan"; $lastName = "Sharma"; $fullName = $firstName . " " . $lastName; // concatenation echo $fullName; // Rohan Sharma $fullName .= "!"; // concatenation assignment echo $fullName; // Rohan Sharma!
It's worth noting that + cannot be used to join strings in PHP the way it can in some other languages — attempting $firstName + $lastName would produce an error or unexpected numeric result, since + is reserved strictly for arithmetic.
Increment and Decrement Operators
These operators increase or decrease a variable's value by exactly one, and are especially common in loops.
php
<?php $count = 5; $count++; // post-increment, $count becomes 6 $count--; // post-decrement, $count becomes 5 again ++$count; // pre-increment, $count becomes 6 --$count; // pre-decrement, $count becomes 5 again
The difference between pre- and post-increment (++$count vs $count++) matters when the operator is used within a larger expression, since PHP evaluates them at different points — but as standalone statements, both produce the same final result.
Operator Precedence
When multiple operators appear in the same expression, PHP follows a specific order of operations, similar to standard math rules (multiplication and division before addition and subtraction, for example).
php
<?php $result = 10 + 5 * 2; // 20, not 30 - multiplication happens first $result2 = (10 + 5) * 2; // 30 - parentheses override default precedence
When in doubt, using parentheses to make the intended order explicit is a good habit — it removes any ambiguity for both PHP and anyone else reading your code later.
Quick Reference Table
Category Example Result ------------------------------------------------------------ Arithmetic 10 % 3 1 Assignment $x += 5 Adds 5 to $x Comparison 5 === "5" false (different types) Logical true && false false String "Hi" . " there" "Hi there" Increment $x++ Increases $x by 1
What's Next
Operators give you the tools to calculate, compare, and combine values, and control flow lets you act on the results. The next logical step is learning how to repeat actions efficiently using loops, which is exactly where the next lesson picks up.