No more writing test scripts when you're not entirely sure. No
more tedious documentation diving. The PHP truth table has finally arrived. I've
prepared the definitive PHP Comparison Cheat Sheet for you. It's your
map for the territory where === doesn't rule.
Since PHP 8 rewrote the comparison rules, there are two tables:
👉 Table for PHP
8.x (The present you need to master)
👉 Table for PHP
7.x (For legacy warriors and code archaeologists)

We've all learned to use === for a good night's sleep.
It's our safety net. But what happens when you don't need to know if values are
identical, but which one is greater or smaller? That's where certainty
evaporates. The operators <, >,
<=, and >= have no “strict” version. PHP
grabs the wheel and fires up type juggling. Do you know for certain how
comparing a number to a string behaves? Or null to
false?
Just check the table and instantly see how types interact when PHP forces
them together. It starts with a comprehensive overview of all operators,
including the spaceship (<=>).
Catch Silent Bugs Before They Strike
Here are two examples that can cost you hours of debugging. Different PHP
functions use different comparison strategies. The sort() function,
for instance, defaults to SORT_REGULAR.
How does it handle strings that look like numbers, such as "042"
and " 42"? How will it sort them?
- I find the SORT_REGULAR section in the table
- I look up the intersection of these values
- I see the symbol
= - What it means: PHP treats them as identical in this mode. The resulting order of these elements after sorting will be undefined. If order matters, we've got a problem.
What about array_unique()? Will it silently “cannibalize” my
data when "042" and " 42" meet in the array? No need
to test.
- The
array_unique()function defaults to SORT_STRING - I check the intersection of these values
- I see the symbol
< - What it means: We're in the clear – the values differ (because everything converts to string)
Thanks to the table, no more guessing. You see instantly when you need to switch flags to make your application behave exactly as intended.
(And yes, there's no flag for strict comparison without type juggling in PHP 😤)
The Fun Stuff: DateTime and Closures
Or what you probably don't know about comparing objects in PHP.
Take DateTime. Many developers believe objects can't be compared, so
they desperately convert dates to timestamps or formatted strings like
'Y-m-d H:i:s' just to figure out what came first. Completely
unnecessary! The DateTime and DateTimeImmutable
classes have built-in logic for standard comparison operators. You can check for
greater/smaller as naturally as with numbers. No helpers, no formatting, just
clean syntax. That's why it deserves its own DateTime section
in the table.
The real fun starts with equality. While === is ruthless with
objects and only cares if you're holding the exact same instance, the
== operator takes a more pragmatic approach with dates – it
compares the time value. This means you can compare two different objects, and
if they represent the same moment in time, PHP says “yes, these are equal”.
Better yet – it works cross-class between DateTime and
DateTimeImmutable!
And the cherry on top? Closures. Even anonymous functions are objects. When are two closures equal? Check the table!
Leave a comment