How to Use Comparison Operators in PHP: Syntax and Examples
In PHP,
comparison operators are used to compare two values and return a boolean result. Common operators include == for equality, != for inequality, and <, > for less than or greater than comparisons.Syntax
Comparison operators compare two values and return true or false. Here are the main operators:
==: Checks if values are equal (ignores type).===: Checks if values and types are identical.!=: Checks if values are not equal.!==: Checks if values or types are not identical.<: Checks if left value is less than right.>: Checks if left value is greater than right.<=: Checks if left value is less than or equal to right.>=: Checks if left value is greater than or equal to right.
php
<?php // Comparison operators syntax examples $a == $b; // Equal $a === $b; // Identical $a != $b; // Not equal $a !== $b; // Not identical $a < $b; // Less than $a > $b; // Greater than $a <= $b; // Less than or equal $a >= $b; // Greater than or equal ?>
Example
This example shows how to use comparison operators to compare numbers and strings, and prints the results.
php
<?php $num1 = 5; $num2 = '5'; // Equal operator (ignores type) if ($num1 == $num2) { echo "$num1 == $num2 is true\n"; } else { echo "$num1 == $num2 is false\n"; } // Identical operator (checks type too) if ($num1 === $num2) { echo "$num1 === $num2 is true\n"; } else { echo "$num1 === $num2 is false\n"; } // Greater than if ($num1 > 3) { echo "$num1 is greater than 3\n"; } // Not equal if ($num1 != 10) { echo "$num1 is not equal to 10\n"; } ?>
Output
$num1 == $num2 is true
$num1 === $num2 is false
$num1 is greater than 3
$num1 is not equal to 10
Common Pitfalls
One common mistake is using == when you want to check both value and type, which can lead to unexpected results because == does type juggling. Always use === to avoid this.
Another pitfall is confusing assignment = with comparison ==. Using = inside an if condition assigns a value instead of comparing, causing bugs.
php
<?php // Wrong: assignment instead of comparison $value = 5; if ($value = 10) { // This assigns 10 to $value, always true echo "This always runs because of assignment\n"; } // Right: comparison if ($value == 10) { echo "This runs only if $value equals 10\n"; } // Wrong: using == instead of === if (0 == '0') { echo "0 == '0' is true (type ignored)\n"; } if (0 === '0') { echo "0 === '0' is true (type checked)\n"; } else { echo "0 === '0' is false\n"; }
Output
This always runs because of assignment
This runs only if $value equals 10
0 == '0' is true (type ignored)
0 === '0' is false
Quick Reference
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal (ignores type) | 5 == '5' | true |
| === | Identical (checks type) | 5 === '5' | false |
| != | Not equal | 5 != 10 | true |
| !== | Not identical | 5 !== '5' | true |
| < | Less than | 3 < 5 | true |
| > | Greater than | 5 > 3 | true |
| <= | Less than or equal | 5 <= 5 | true |
| >= | Greater than or equal | 5 >= 3 | true |
Key Takeaways
Use
=== to compare both value and type to avoid unexpected results.Remember that
== compares values but ignores type differences.Never confuse assignment
= with comparison == inside conditions.Comparison operators return boolean
true or false based on the comparison.Use the quick reference table to pick the right operator for your comparison needs.