PHP Comparison Operators
PHP Comparison Operators
PHP comparison operators are used to compare two values and determine whether a specific condition is true or false. They are commonly used in if statements, loops, conditional expressions, and application business logic.
Understanding comparison operators is essential for PHP developers because they help programs make decisions based on values, data types, and relationships between variables.
What Are Comparison Operators in PHP?
Comparison operators compare two expressions or values. The result of a comparison is a Boolean value: true or false.
For example:
<?php
$a = 10;
$b = 20;
var_dump($a < $b);
?>
Output:
bool(true)
Because 10 is less than 20, the comparison returns true.
PHP Comparison Operators List
| Operator | Name | Description | Example |
|---|---|---|---|
== |
Equal | Checks whether two values are equal after type juggling. | $a == $b |
=== |
Identical | Checks whether two values are equal and have the same data type. | $a === $b |
!= |
Not Equal | Checks whether two values are not equal. | $a != $b |
<> |
Not Equal | Another syntax for not equal. | $a <> $b |
!== |
Not Identical | Checks whether values or their data types are different. | $a !== $b |
< |
Less Than | Checks whether the left value is less than the right value. | $a < $b |
> |
Greater Than | Checks whether the left value is greater than the right value. | $a > $b |
<= |
Less Than or Equal To | Checks whether the left value is less than or equal to the right value. | $a <= $b |
>= |
Greater Than or Equal To | Checks whether the left value is greater than or equal to the right value. | $a >= $b |
<=> |
Spaceship | Compares two values and returns -1, 0, or 1. | $a <=> $b |
1. Equal Operator (==)
The == operator checks whether two values are equal. PHP may convert the values to compatible types before performing the comparison.
<?php
$a = 10;
$b = "10";
var_dump($a == $b);
?>
Output:
bool(true)
The integer 10 and the string "10" compare as equal with ==.
2. Identical Operator (===)
The === operator checks both the value and the data type. Both must match for the comparison to return true.
<?php
$a = 10;
$b = "10";
var_dump($a === $b);
?>
Output:
bool(false)
The values look the same, but one is an integer and the other is a string. Therefore, they are not identical.
3. Not Equal Operator (!=)
The != operator returns true when two values are not equal.
<?php
$a = 10;
$b = 20;
var_dump($a != $b);
?>
Output:
bool(true)
4. Not Equal Operator (<>)
The <> operator is another form of the != operator.
<?php
$a = 10;
$b = 20;
var_dump($a <> $b);
?>
The result is true because the two values are different.
5. Not Identical Operator (!==)
The !== operator returns true when two values are different or when their data types are different.
<?php
$a = 10;
$b = "10";
var_dump($a !== $b);
?>
Output:
bool(true)
The values are numerically equivalent, but their types are different, so they are not identical.
6. Less Than Operator (<)
The < operator checks whether one value is smaller than another.
<?php
$age = 18;
var_dump($age < 21);
?>
Output:
bool(true)
7. Greater Than Operator (>)
The > operator checks whether the left-hand value is greater than the right-hand value.
<?php
$salary = 50000;
var_dump($salary > 30000);
?>
Output:
bool(true)
8. Less Than or Equal To Operator (<=)
The <= operator checks whether a value is smaller than or equal to another value.
<?php
$marks = 50;
var_dump($marks <= 50);
?>
Output:
bool(true)
9. Greater Than or Equal To Operator (>=)
The >= operator checks whether a value is greater than or equal to another value.
<?php
$age = 21;
var_dump($age >= 18);
?>
Output:
bool(true)
10. Spaceship Operator (<=>)
The spaceship operator, <=>, compares two values and returns:
| Result | Meaning |
|---|---|
-1 |
Left value is less than the right value |
0 |
Both values are equal |
1 |
Left value is greater than the right value |
Example:
<?php
echo 10 <=> 20;
echo "<br>";
echo 20 <=> 20;
echo "<br>";
echo 30 <=> 20;
?>
Output:
-1
0
1
The spaceship operator is particularly useful for implementing comparison logic and sorting.
== vs === in PHP
One of the most important concepts for PHP developers is understanding the difference between == and ===.
| Feature | == |
=== |
|---|---|---|
| Checks value | Yes | Yes |
| Checks data type | No | Yes |
| Type conversion may occur | Yes | No |
| Strict comparison | No | Yes |
Example:
<?php
$a = 5;
$b = "5";
var_dump($a == $b); // true
var_dump($a === $b); // false
?>
For many application-level comparisons, === is preferred when you need both the value and type to match explicitly.
Using Comparison Operators with if Statements
Comparison operators are frequently used with conditional statements.
<?php
$age = 25;
if ($age >= 18) {
echo "You are eligible.";
} else {
echo "You are not eligible.";
}
?>
Here, the >= operator determines whether the person’s age is at least 18.
Using Multiple Comparison Conditions
Comparison operators can be combined with logical operators such as && and ||.
<?php
$age = 25;
$country = "India";
if ($age >= 18 && $country === "India") {
echo "Condition matched.";
}
?>
Real-World Example: Login Validation
Comparison operators are useful when validating user information.
<?php
$enteredUsername = "admin";
$correctUsername = "admin";
if ($enteredUsername === $correctUsername) {
echo "Username is valid.";
} else {
echo "Invalid username.";
}
?>
The strict comparison ensures that the values and their data types are both appropriate for the comparison.
Real-World Example: Product Price Validation
<?php
$price = 999;
if ($price <= 1000) {
echo "Product is within budget.";
} else {
echo "Product exceeds budget.";
}
?>
Common Mistakes with PHP Comparison Operators
Using = Instead of ==
The = operator performs assignment, while == performs comparison.
<?php
$x = 10; // Assignment
if ($x == 10) {
echo "Equal";
}
?>
Confusing == with ===
== performs a non-strict comparison, while === performs a strict comparison. Choosing the appropriate operator can prevent unexpected behavior caused by type conversion.
Ignoring Data Types
Values originating from forms, query parameters, cookies, APIs, or databases may not always have the data type you expect. Developers should consider data types when writing comparison logic.
PHP Comparison Operators Example
<?php
$a = 10;
$b = 20;
echo ($a == $b) ? "Equal" : "Not Equal";
echo "<br>";
echo ($a < $b) ? "A is smaller" : "A is not smaller";
echo "<br>";
echo ($a > $b) ? "A is greater" : "A is not greater";
echo "<br>";
echo ($a <= $b) ? "A is less than or equal to B" : "A is greater than B";
echo "<br>";
echo ($a <=> $b);
?>
Best Practices for Using PHP Comparison Operators
- Use
===when both value and data type should match. - Use
!==when strict inequality is required. - Use
==only when PHP’s type conversion is intentionally acceptable. - Choose comparison operators that clearly express the business condition.
- Be careful when comparing values received from external input.
- Use parentheses when combining multiple conditions to make the intended logic clear.
Conclusion
PHP comparison operators are fundamental to writing conditional and decision-making logic. Operators such as ==, ===, !=, !==, <, >, <=, >=, and <=> allow developers to compare values in different ways.
The key distinction to remember is that == performs a non-strict comparison, while === performs a strict comparison that also considers data type. A strong understanding of these operators helps PHP developers write reliable, predictable, and maintainable applications.
Frequently Asked Questions About PHP Comparison Operators
What are comparison operators in PHP?
Comparison operators are PHP operators used to compare two values and return a Boolean result or, in the case of the spaceship operator, a comparison result of -1, 0, or 1.
What is the difference between == and === in PHP?
== compares values with type juggling, whereas === compares both value and data type without type conversion.
Which comparison operator should I generally prefer in PHP?
When you require an exact match of both value and type, === is generally the clearer choice.
What does the PHP spaceship operator do?
The <=> operator compares two values and returns -1 when the first is smaller, 0 when they are equal, and 1 when the first is greater.
—
PHP Tutorials
PHP Tutorials on this website:
More Information on PHP