Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up

What is the difference between == and === in PHP, and how does type juggling work?

=== compares type and value. == converts the operands to a common type first, following rules that produce genuinely surprising results.

PHP 8 fixed the worst of it. Comparing a number to a non-numeric string used to cast the string to a number, so 0 == "foo" was true — a real authentication bypass in the wild. Since PHP 8, the number is cast to a string instead, so that comparison is now false.

What still catches people:

  • "1" == "01" is true — two numeric strings are compared as numbers.
  • "10" == "1e1" is true, for the same reason.
  • null == false, 0 == false, and "" == false are all true.
  • [] == false is true.

The practical rule: use === by default. It is not a style preference — loose comparison against user input has caused real security vulnerabilities, particularly when comparing tokens or hashes. For those, use hash_equals(), which is also timing-safe.

Note: Be ready for in_array() as a follow-up. Its third parameter enables strict comparison, and without it in_array(0, ['a','b']) behaved surprisingly on older versions.

All Php interview questions

Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up as