How to Compare Values in PHP with === (Strict Comparison)
PHP has two equality operators. == converts the values to a common type before comparing them, which sometimes calls different things equal. === compares the value and the type, so it's only true when both match exactly.
Syntax
$<a> === $<b>
$<a> !== $<b>
| Part | What it does |
|---|---|
=== | true when both sides have the same value and the same type. |
!== | true when the value or the type differs. |
== | true when the values are equal after PHP converts them, "type juggling". |
The PHP manual on comparison operators defines $a === $b as "true if $a is equal to $b, and they are of the same type", and $a == $b as "true if $a is equal to $b after type juggling."
The Example
The example compares three pairs of values twice, once with == and once with ===, and prints each result with var_dump.
Prerequisites
- PHP for the command line. This guide uses PHP 8.6.0.
Step 1: Write the Comparisons
Create a file named main.php:

<?php
var_dump(0 == '0');
var_dump(0 === '0');
var_dump('1e3' == '1000');
var_dump('1e3' === '1000');
var_dump(null == false);
var_dump(null === false);
Step 2: Run It
php main.php
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)
bool(false)

0 == '0'istrue: the string is converted to a number. With===, anintand astringdiffer.'1e3' == '1000'istrue: both strings look like numbers, so PHP compares them as numbers, and1e3is 1000.null == falseistrue, butnullandfalseare different types.
The command is the same in zsh, bash and PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Comparing user input with == | Different strings such as '1e3' and '1000' count as equal | Use === |
Testing a function's result with == false | 0, '' and '0' also match | Compare with === false |
| Expecting old PHP 7 results | PHP 8 compares a number with a non-numeric string as strings, so 0 == 'a' is false | Don't rely on == between numbers and text |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Two different-looking values compare as equal | == converted them | Switch to === |
| A check fails although the values look the same | One is an int, the other a string | Convert one side, or compare with == on purpose |
FAQ
Is there a strict "not equal"?
Yes: !== is true when the value or the type differs.
Does === compare arrays?
Yes: two arrays are identical when they have the same key and value pairs, in the same order and of the same types.
Conclusion
=== compares the value and the type, so it never converts anything; == converts first and can call different values equal. Use === by default.
Additional Resources
- Comparison operators, in the PHP manual.
