Introduction: Why PHP Operators Matter
In any programming language, operators play a key role in performing operations on variables and values. PHP is no different.
From simple calculations to complex logic handling, PHP operators help you write expressive and functional code. Mastering them is essential for every PHP developer to build dynamic web applications effectively.
In this article, you'll learn:
-
The different types of operators in PHP
-
Their syntax and usage with examples
-
Practical tips and mistakes to avoid
Types of PHP Operators
PHP supports a rich set of operators. Here's a categorized breakdown:
➕ 1. Arithmetic Operators
These are used for basic mathematical operations.
Operator | Name | Example | Result |
---|---|---|---|
+ |
Addition | 2 + 3 |
5 |
- |
Subtraction | 5 - 2 |
3 |
* |
Multiplication | 4 * 3 |
12 |
/ |
Division | 10 / 2 |
5 |
% |
Modulus | 7 % 2 |
1 |
** |
Exponentiation | 2 ** 3 |
8 |
2. Assignment Operators
Used to assign values to variables.
Operator | Example | Equivalent |
---|---|---|
= |
$a = 5 |
Assign 5 to $a |
+= |
$a += 2 |
$a = $a + 2 |
-= |
$a -= 2 |
$a = $a - 2 |
*= |
$a *= 2 |
$a = $a * 2 |
/= |
$a /= 2 |
$a = $a / 2 |
%= |
$a %= 2 |
$a = $a % 2 |
3. Comparison Operators
Used to compare two values (returns boolean).
Operator | Description | Example | Result |
---|---|---|---|
== |
Equal | 5 == "5" |
true |
=== |
Identical (type + value) | 5 === "5" |
false |
!= |
Not equal | 5 != 4 |
true |
!== |
Not identical | 5 !== "5" |
true |
> |
Greater than | 5 > 3 |
true |
< |
Less than | 3 < 5 |
true |
>= |
Greater than or equal to | 5 >= 5 |
true |
<= |
Less than or equal to | 4 <= 5 |
true |
<=> |
Spaceship operator | 3 <=> 4 |
-1 |
4. Logical Operators
Used to combine conditional statements.
Operator | Description | Example |
---|---|---|
and |
True if both are true | $a and $b |
or |
True if one is true | $a or $b |
xor |
True if one (not both) | $a xor $b |
&& |
And | $a && $b |
` | ` | |
! |
Not | !$a |
5. Increment/Decrement Operators
Operator | Description | Example |
---|---|---|
++$x |
Pre-increment | Increments then returns |
$x++ |
Post-increment | Returns then increments |
--$x |
Pre-decrement | Decrements then returns |
$x-- |
Post-decrement | Returns then decrements |
6. String Operators
Operator | Description | Example |
---|---|---|
. |
Concatenation | "Hello " . "World" |
.= |
Concatenation assignment | $msg .= "World" |
7. Array Operators
Operator | Name | Example | Result |
---|---|---|---|
+ |
Union | $a + $b |
Merges arrays |
== |
Equality | $a == $b |
True if equal |
=== |
Identity | $a === $b |
True if same order/type |
!= |
Inequality | $a != $b |
True if not equal |
<> |
Inequality | $a <> $b |
Same as != |
!== |
Non-identity | $a !== $b |
True if not identical |
8. Conditional (Ternary) Operator
$age = 20;
$status = ($age >= 18) ? "Adult" : "Minor";
The above is shorthand for:
if ($age >= 18) {
$status = "Adult";
} else {
$status = "Minor";
}
Complete Functional Code Example
<?php
$a = 10;
$b = 3;
// Arithmetic
echo "Addition: " . ($a + $b) . "\n";
echo "Division: " . ($a / $b) . "\n";
// Comparison
echo "Is Equal: " . var_export($a == $b, true) . "\n";
// Logical
if ($a > 5 && $b < 5) {
echo "Both conditions are true.\n";
}
// Ternary
echo ($a > $b) ? "A is greater\n" : "B is greater\n";
// String
$str1 = "Hello";
$str2 = "World";
echo $str1 . " " . $str2 . "\n";
?>
⚠️ Tips & Common Pitfalls
✅ Tips
-
Use
===
instead of==
for type-safe comparisons -
Prefer
&&
and||
overand
/or
for clarity and precedence -
Use spaces around operators for better readability
❌ Common Pitfalls
-
Confusing
=
(assignment) with==
(comparison) -
Using
or
/and
without parentheses can lead to unexpected results due to lower precedence
$result = true || false; // Correct
$result = true or false; // Wrong if used without parentheses
Conclusion & Best Practices
PHP operators are the building blocks of logic and control flow in your scripts. By understanding their syntax, behavior, and precedence, you write code that is both clean and predictable.
Key Takeaways
-
Use arithmetic and assignment operators for calculations
-
Master comparison and logical operators for control structures
-
Know the difference between
==
vs===
, and&&
vsand
-
Be cautious with operator precedence
By practicing these operators in real-world scenarios, you’ll quickly gain confidence in writing PHP logic that works as expected.