Menu

PHP Comparison Operators: == vs === and <=>

PHP compares values with == (equal after type juggling), === (equal value and same type), != and !==, < > <= >= and the spaceship operator <=>. Learn when == converts types, what changed in PHP 8, and see every loose and strict comparison in one table.

This page includes runnable editors - edit, run, and see output instantly.

== 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

OperatorNameTrue when
$a == $bEqualequal after type juggling
$a === $bIdenticalequal and the same type
$a != $b, $a <> $bNot equalnot equal after type juggling
$a !== $bNot identicalnot equal, or different types
$a < $b, $a > $bLess than, greater than
$a <= $b, $a >= $bLess or equal, greater or equal
$a <=> $bSpaceshipreturns -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.

Loose (==) and strict (===) comparison
Point at or tap a cell to read it as PHP.
$a == $btruefalse10-1"1""0"""null[]"php""10""1e1"
true
false
1
0
-1
"1"
"0"
""
null
[]
"php"
"10"
"1e1"
Every cell computed by PHP 8.3.19.

How == compares different types

The rules PHP 8 follows, in the order they matter in practice:

  1. Two numeric strings are compared as numbers: "10" == "1e1" and "1" == "01" are both true.
  2. A number and a numeric string: the string becomes a number. 100 == "1e2" is true.
  3. A number and a non-numeric string: since PHP 8, the number becomes a string and they are compared as strings. 0 == "a" is false (it was true in PHP 7).
  4. Anything and a bool: both sides are converted to bool. "a" == true and [] == false are true.
  5. null and a string: null becomes "", so null == "" is true but null == "0" is false. Against any other type, null is compared as false.
  6. 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.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED