== checks whether two values are equal after PHP converts them to a common type. === checks that they are equal and of the same type. So 1 == "1" is true, while 1 === "1" is false.
Most bugs with comparisons come from == converting something you did not expect. A good default is === and !==, switching to == only when you know you want the conversion.
All PHP comparison operators
| Operator | Name | True when |
|---|---|---|
$a == $b | Equal | equal after type juggling |
$a === $b | Identical | equal and the same type |
$a != $b, $a <> $b | Not equal | not equal after type juggling |
$a !== $b | Not identical | not equal, or different types |
$a < $b, $a > $b | Less than, greater than | |
$a <= $b, $a >= $b | Less or equal, greater or equal | |
$a <=> $b | Spaceship | returns -1, 0 or 1 |
Loose and strict comparison table
Every cell of this table was computed by PHP 8.3. Switch between == and === and tap a cell to see the expression. The == grid is full of surprises in the rows for null, "", "0" and []; the === grid is just its diagonal.
| $a == $b | true | false | 1 | 0 | -1 | "1" | "0" | "" | null | [] | "php" | "10" | "1e1" |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| true | |||||||||||||
| false | |||||||||||||
| 1 | |||||||||||||
| 0 | |||||||||||||
| -1 | |||||||||||||
| "1" | |||||||||||||
| "0" | |||||||||||||
| "" | |||||||||||||
| null | |||||||||||||
| [] | |||||||||||||
| "php" | |||||||||||||
| "10" | |||||||||||||
| "1e1" |
How == compares different types
The rules PHP 8 follows, in the order they matter in practice:
- Two numeric strings are compared as numbers:
"10" == "1e1"and"1" == "01"are bothtrue. - A number and a numeric string: the string becomes a number.
100 == "1e2"istrue. - A number and a non-numeric string: since PHP 8, the number becomes a string and they are compared as strings.
0 == "a"isfalse(it wastruein PHP 7). - Anything and a bool: both sides are converted to bool.
"a" == trueand[] == falsearetrue. - null and a string:
nullbecomes"", sonull == ""istruebutnull == "0"isfalse. Against any other type,nullis compared asfalse. - Two arrays: equal when they have the same key/value pairs.
That last line shows why ordering comparisons with null are rarely meaningful: null becomes false, -1 becomes true, and false < true.
Not equal: != vs !==
!= and <> are the same operator. !== is the strict version. The difference matters most with functions that return 0 or false, like strpos:
0 != false is false because 0 converts to false. 0 !== false is true because an int is not a bool. The same trap applies to array_search and in_array without its strict flag.
Compare strings
< and > compare strings byte by byte (alphabetically for plain ASCII). If both strings are numeric, they are compared as numbers. For case-insensitive or "natural" order use the functions:
The spaceship operator <=>
$a <=> $b returns -1 when $a is smaller, 0 when they are equal and 1 when $a is larger. It uses the same rules as < and ==, and it is the shortest way to write a sort callback.
Comparing two arrays with <=> compares them element by element, which is how the callback sorts by age first and name second. More sorting recipes are on the sort arrays page.
Common mistake: comparing floats with ==
Floats are stored in binary, so many decimal values are approximations. 0.1 + 0.2 is not exactly 0.3, and == sees the difference.
For money, store whole cents as integers and compare those with ===.
Frequently Asked Questions
What is the difference between == and === in PHP?
== converts both sides to a common type and then compares, so 1 == "1" is true. === is true only when the values are equal and of the same type, so 1 === "1" is false. Use === unless you want the conversion.
Is 0 == "a" true in PHP?
Not since PHP 8. When a number is compared with a string that is not numeric, PHP 8 compares them as strings, so 0 == "a" is false. In PHP 7 the string was cast to 0 and the result was true.
What is the difference between != and !== in PHP?
!= (and its alias <>) is the opposite of ==: it is false when the values are equal after type conversion. !== is the opposite of ===: it is true when the values differ or their types differ, so 0 !== false is true while 0 != false is false.
How do I compare two floats in PHP?
Do not use == on results of float arithmetic: 0.1 + 0.2 == 0.3 is false. Compare the difference with a small tolerance instead: abs($a - $b) < PHP_FLOAT_EPSILON, or round both to the precision you care about.