Loose Equality vs Strict Identity in PHP
What is the difference between `==` and `===` in PHP, and when would you use each one?
what they're testing: The interviewer is probing whether you understand PHP's comparison coercion and can avoid bugs caused by values with different types comparing as equal.
== compares values after PHP applies its comparison conversion rules. === requires the same value and the same type, with no type juggling. So '5' == 5 is true, while '5' === 5 is false. Likewise, 0 == false is true, but 0 === false is false.
That makes === the safer default when type is part of the contract. A common example is strpos(), which returns 0 when a match starts at the first character and false when there is no match. Checking $position == false confuses those results. $position === false distinguishes them correctly. Use == only when cross-type equivalence is deliberate, such as comparing validated numeric input with a number. Even then, an explicit conversion often makes the intent clearer.
Objects have a separate rule. For ordinary objects, == can consider two instances of the same class equal when their properties and property values compare equal. Extensions can define different comparison behavior for their objects. With ===, both variables must refer to the same object instance.
Where people slip
the tempting wrong answer, and what's actually true
`==` ignores types and compares the raw values directly.
`==` may convert operands according to PHP's type-juggling rules before comparing them.
`===` compares memory addresses for every kind of value.
`===` requires matching values and types for non-objects; only objects use the same-instance rule.
Checking `strpos()` with `== false` is safe because `0` also means no match.
`strpos()` returns `0` for a match at the first character and `false` for no match, so the check must be strict.
If they push further
What do `'0' == false` and `'0' === false` return?
The loose comparison is `true` because PHP converts both operands to booleans, and `'0'` converts to `false`. The strict comparison is `false` because the operands have different types.
How do these operators behave with objects?
For ordinary objects, `==` compares same-class instances by their properties, using loose comparison for property values. Extensions may define different rules. `===` is true only when both variables refer to the same instance.
Does `declare(strict_types=1)` make `==` behave like `===`?
No. Strict typing changes coercion in typed function contexts; it does not redefine comparison operators.
Sources
- PHP Manual: Comparison Operators ↗php.net
- PHP Manual: Type Juggling ↗php.net
- PHP Manual: Comparing Objects ↗php.net
- PHP Manual: strpos ↗php.net
- PHP Manual: Type Declarations and Strict Typing ↗php.net
Now answer it yourself.
Reading a strong answer is easy. Producing one under pressure is the skill the interview tests. Gapmap grades your answer against the same bar an interviewer would.
beta
The interviewer part is in the works.
The diagnostic, personal maps, and AI mock interviews are being finished right now. The notes stay free either way. Leave an email and you'll get the first-cohort invite, plus a month of Pro when it opens.