PHP Comparison Operators: ==, ===, and Sorting
Use === in PHP when both the value and its type must match. == may convert types before comparing, so it considers the integer 123 equal to the string '123'. Use <, <=, >, and >= for order and range checks and <=> when you need an integer indicating relative order.
Comparison operator reference
| Operator | Result |
|---|---|
== |
True if the values match after any type conversion |
=== |
True if both value and type match |
!= / <> |
True if the values differ under loose comparison. Both spellings are equivalent. |
!== |
True if the value or the type differs |
< / > |
True if the left value is less than or greater than the right, respectively |
<= / >= |
True if the left value is less than or equal to, or greater than or equal to, the right, respectively |
<=> |
Negative if the left value is smaller, zero if equal, positive if larger |
PHP comparison operators manual
= assigns the value on the right to the variable on the left. == and === return comparison results and are used to test equality.
Loose and strict equality
Save the example as compare.php and run php compare.php in a terminal with PHP 8 installed. It compares different types, then shows how numeric strings and booleans behave.
<?php
var_dump(123 == '123');
var_dump(123 === '123');
var_dump(123 != '123', 123 <> '123');
var_dump(123 !== '123');
var_dump('00123' == '123');
var_dump(false == '0');
var_dump(0 == 'ready');
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
The first two lines differ because of type conversion. == compares the integer 123 and the numeric string '123' numerically. === returns false because their types differ. != and <> invert ==, while !== inverts ===. When both strings are numeric, as with '00123' and '123', == considers them equal.
The last two lines show why the types still matter in PHP 8. When one operand is a boolean, the other is converted to a boolean, so false == '0' is true. PHP 7 evaluates 0 == 'ready' as true, whereas PHP 8 returns false. That version change concerns loose comparisons between numbers and non-numeric strings.
Comparison changes when migrating to PHP 8
Range checks and sorting with <=>
Save this example as order.php and run php order.php. The first expression checks whether the score is between 70 and 100, including both endpoints. usort() orders the array using the sign of the integer returned by the comparison function.
<?php
$score = 70;
var_dump($score >= 70 && $score <= 100);
var_dump(10 <=> 20, 20 <=> 20, 30 <=> 20);
$scores = [82, 70, 90];
usort($scores, static function (int $a, int $b): int {
return $a <=> $b;
});
echo implode(', ', $scores), PHP_EOL;
bool(true)
int(-1)
int(0)
int(1)
70, 82, 90
The score 70 is in range. With these integer operands, <=> returns -1, 0, and 1, and the sorted list is 70, 82, 90. Change the callback to return $b <=> $a to see descending order. usort() modifies the original array and reindexes it, so choose a key-preserving sort when the original keys matter.
Choosing the right operator
- Compare product codes as strings when leading zeros matter.
'00123' === '123'is false. - For quantities, check the input format and allowed range first, then convert both values to the same numeric type before comparing.
- Choose a relational operator such as
<or>=for a condition. Use the sign returned by<=>to tell a sorting function how two values should be ordered.




