Operators in PHP
What are Operators in PHP?
Operators are symbols that tell PHP to perform a specific action on one or more values.
For example:
$x = 10 + 5; // '+' is an operator
Here, + tells PHP to add two values.
Arithmetic Operators in PHP
Arithmetic operators are used to perform mathematical calculations.
Common Arithmetic Operators
Operator | Description | Example + | Addition | $a + $b - | Subtraction | $a - $b * | Multiplication | $a * $b / | Division | $a / $b % | Modulus | $a % $b
Example
$a = 10; $b = 3; echo $a + $b; // 13 echo $a - $b; // 7 echo $a * $b; // 30 echo $a / $b; // 3.33 echo $a % $b; // 1
Use case:
Used in calculations like totals, discounts, percentages, and counters.
Important:
Always ensure the divisor is not zero when performing division, as dividing by zero will cause a fatal error in PHP.
Modulus explained:
The modulus operator returns the remainder after division. It is commonly used to check if a number is even or odd, or to cycle through values (for example, alternating items in a loop).
Comparison Operators in PHP
Comparison operators are used to compare two values. They always return a boolean value (true or false).
Common Comparison Operators
Operator | Meaning | Example -------- | ------------------------ | ----------- == | Equal (value only) | $a == $b === | Identical (value + type) | $a === $b != | Not equal | $a != $b !== | Not identical | $a !== $b > | Greater than | $a > $b < | Less than | $a < $b >= | Greater or equal | $a >= $b <= | Less or equal | $a <= $b
Example
$a = 10; $b = "10"; var_dump($a == $b); // true var_dump($a === $b); // false
Use case:
Validating user input, checking login credentials, or comparing values in conditions.
Logical Operators in PHP
Logical operators are used to combine multiple conditions.
Common Logical Operators
Operator | Meaning | Example -------- | ----------------------- | ------------ && | AND (both must be true) | $a && $b || | OR (at least one true) | $a || $b ! | NOT (reverse condition) | !$a
Example
$age = 20;
$isLoggedIn = true;
if ($age > 18 && $isLoggedIn) {
echo "Access granted";
}
Use case:
Used in decision-making like login systems, permissions, and validations.
Operator Precedence (Important Concept)
When multiple operators are used in one expression, PHP follows a priority order.
$result = 10 + 5 * 2; // Output: 20
Multiplication happens before addition.
To control order, use parentheses:
$result = (10 + 5) * 2; // Output: 30
Common Mistakes Beginners Make
1. Using = Instead of ==
if ($a = 10) { // Incorrect
Why it fails:
This assigns a value instead of comparing, which breaks logic.
2. Confusing == and ===
var_dump("5" == 5); // true
var_dump("5" === 5); // false
Why it matters:
Loose comparison can produce unexpected results.
3. Ignoring Operator Precedence
$result = 10 + 5 * 2; // Not 30
Why it matters:
Assuming left-to-right evaluation leads to incorrect results.
4. Mixing && and || Without Parentheses
if ($age > 18 && $isLoggedIn || $isAdmin) {
Why it’s dangerous:
PHP evaluates && before ||, which may allow unintended access.
Correct version:
if (($age > 18 && $isLoggedIn) || $isAdmin) {
Real-World Example
$price = 100;
$discount = 20;
$isMember = true;
// Apply discount
$total = $price - $discount;
// Check access condition
if ($isMember && $total > 50) {
echo "Final Price: ₹" . $total;
}
Output:
Final Price: ₹80
What this shows:
- Arithmetic (
-) - Comparison (
>) - Logical (
&&)
Practice Exercise
Task 1
Add two numbers and print the result.
Expected Output:
15
Task 2
Compare two values using == and ===.
Expected Output:
true false
Task 3
Write a condition using &&.
Expected Output:
Access granted
Summary
In this lesson, you learned how operators work in PHP. Arithmetic operators handle calculations, comparison operators evaluate values, and logical operators combine conditions to control decision-making in your applications.