Introduction
Once you understand PHP variables and data types, the next important step is learning how to work with values. In PHP, this is done using operators and expressions. Operators allow you to perform actions like addition, comparison, and logical checks, while expressions are combinations of values and operators that produce a result.
If you’re building dynamic features for websites or working with platforms like WordPress, you’ll use operators in almost every script you write.
In this tutorial, you’ll learn the most common PHP operators with simple examples and understand how expressions work in real-world PHP code.
What Are Operators in PHP?
An operator is a symbol that tells PHP to perform a specific operation on one or more values (operands).
<?php
$a = 10;
$b = 5;
$sum = $a + $b;
echo $sum; // 15
?>
In the above example, $a and $b are variables that act as operands, while + functions as the operator.
A combination of operators and operands is called an expression.
Types of Operators in PHP with Examples
PHP provides different types of operators for different operations.
1️⃣ Arithmetic Operators
Arithmetic operators are used for mathematical calculations.
| Operator | Description |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
** | Exponentiation |
Example:
<?php
$a = 10;
$b = 3;
echo $a + $b; // Addition
echo $a - $b; // Subtraction
echo $a * $b; // Multiplication
echo $a / $b; // Division
echo $a % $b; // Modulus (remainder)
?>
2️⃣ Assignment Operators
Assignment operators assign values to variables.
| Operator | Description |
|---|---|
= | Assign |
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
/= | Divide and assign |
Example:
<?php
$x = 10;
$x += 5; // Same as $x = $x + 5;
$x -= 2;
$x *= 3;
$x /= 2;
?>
3️⃣ Comparison Operators
Comparison operators are used to compare values.
| Operator | Description |
|---|---|
== | Equal |
=== | Identical |
!= | Not equal |
!== | Not identical |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
Example:
<?php
$a = 10;
$b = 20;
var_dump($a == $b); // equal
var_dump($a != $b); // not equal
var_dump($a > $b); // greater than
var_dump($a < $b); // less than
var_dump($a >= $b); // greater or equal
var_dump($a <= $b); // less or equal
?>
4️⃣ Logical Operators
Logical operators are used to combine conditions.
| Operator | Description |
|---|---|
&& | AND |
| || | OR |
! | NOT |
Example:
<?php
$age = 20;
$isMember = true;
if ($age >= 18 && $isMember) {
echo "Access granted";
}
?>
5️⃣ Increment and Decrement Operators
These operators increase or decrease a value by one.
Used for loops and counters.
| Operator | Description |
|---|---|
++ | Increment |
-- | Decrement |
Example:
<?php
$count = 5;
$count++; // 6
$count--; // 5
?>
6️⃣ String Operators
PHP provides operators to work with strings.
| Operator | Description |
|---|---|
. | Concatenation |
.= | Concatenate and assign |
Example:
<?php
$first = "Hello";
$second = "PHP";
echo $first . " " . $second;
?>
Difference Between == and === in PHP
PHP has two ways to compare values:
✅ == (Loose Comparison)
This compares values only, not data types.
<?php
var_dump(10 == "10"); // true
?>
Why?
PHP automatically converts "10" (string) to 10 (integer) before comparing.
Use == when:
- You only care about the value
- Type does not matter (not recommended for most cases)
✅ === (Strict Comparison)
This compares both value and data type.
<?php
var_dump(10 === "10"); // false
?>
Why?
Here, 10 is an integer and "10" is a string, so the types are different.
<?php
$userId = 0;
if ($userId == false) {
echo "User not found (loose check)";
}
if ($userId === false) {
echo "User not found (strict check)";
}
?>
Here, $userId == false may behave unexpectedly because 0 can be treated as false in loose comparisons.
Using === avoids this confusion.
What Are Expressions?
An expression is any valid combination of values, variables, and operators that results in a value.
Examples:
<?php
$total = 10 + 5; // Expression
$isAdult = ($age >= 18); // Expression
$message = "Hello " . "World"; // Expression
?>
Operator Precedence (Basic Idea)
Some operators are executed before others.
Example:
<?php
$result = 10 + 5 * 2;
echo $result; // 20
?>
Multiplication happens before addition.
Use parentheses to control precedence:
<?php
$result = (10 + 5) * 2;
echo $result; // 30
?>
Best Practices When Using Operators
- Use
===instead of==when possible - Use parentheses for clarity
- Avoid complex expressions in one line
- Keep code readable
Common Beginner Mistakes
1. Confusing = and === assigns a value. == compares values.
2. Forgetting operator precedence
Use parentheses to avoid logic bugs.
3. Using strings and numbers incorrectly"10" + 5 works in PHP but can cause confusion.
4. Overcomplicating expressions
Break long expressions into smaller parts for readability.
Conclusion
Operators and expressions are essential for writing logical and dynamic PHP code.
Understanding them clearly is necessary before learning conditions and loops.
In the next tutorial, we will learn about PHP Conditional Statements (if, else, switch).
