Interview frequency: …
Short answer
== compares values after attempting type conversion, while === compares both value and type without implicit conversion.
Explanation
In PHP versions before PHP 8, implicit comparisons with == caused security problems (for example, 0 == 'admin' returned true). PHP 8 made comparisons between numbers and strings more careful, but the standard interview advice is still to use === everywhere to avoid surprises from implicit conversions.
Code example
0 === "0"; // false
0 == "0"; // true
false === 0; // falseCommon mistakes
- Using == to compare passwords or security tokens, allowing guessing vulnerabilities and Type Juggling.
Follow-up questions
- What is the difference between Type Juggling and declare(strict_types=1) in PHP?
- What is the difference between the Null Coalescing operator (??) and the Nullsafe Operator (?->) in modern PHP?