PHP 7 interview question · Most asked
What are type-juggling vulnerabilities in PHP 7 and how do you prevent them?
Summary
In PHP 7, loose comparison (==) converts types unpredictably: '0e123' == '0e456' is true (both read as 0 in scientific notation) and 0 == 'abc' is true. This enables authentication bypasses with 'magic hashes'. Always use === and hash_equals().
Longer Answer
Classic bug: if (md5($input) == $storedHash), where both hashes start with 0e followed by digits, so they compare equal as floats (0). Also in_array, array_search and switch use loose comparison by default. Use strict comparison, strict flags and timing-safe hash_equals() for secrets.
Key Points
'0e1' == '0e2'is true (numeric strings)0 == 'abc'true in PHP 7- Use
===,in_array(..., true) hash_equals()for token comparison
More most-asked PHP 7 questions
- What are typed properties in PHP 7.4?
- How do return type declarations work in PHP 7?
- What were the headline improvements of PHP 7.0 over PHP 5.6?
- Explain scalar type declarations and coercive vs strict mode in PHP 7.
- Explain the `Throwable`, `Error` and `Exception` hierarchy introduced in PHP 7.
- What are nullable types in PHP 7.1?
- What does the null coalescing operator `??` do, and how is it different from `isset()` with a ternary?
- What are arrow functions in PHP 7.4 and how do they differ from closures?