Skip to content
CoursesWeb Forum

PHP == or ===, why is my string equal to zero

Asked by Mpho N. · PHP Forms, Sessions and Basics2 repliesupdated

Short answer

Our lecturer says "always use ===" but never says why, so I tested it. On my laptop (PHP 8.3):

Written by Mpho N. ·
CiteCoursesWeb Forum, "PHP == or ===, why is my string equal to zero", https://coursesweb.net/forum/t/php-or-why-is-my-string-equal-to-zero/, accessed 2026-12-06.
  1. Member ·
    var_dump("abc" == 0);   // bool(false)
    var_dump("1" == "01");  // bool(true)
    var_dump("10" == "1e1"); // bool(true)
    var_dump(null == false); // bool(true)
    

    My classmate runs an old XAMPP with PHP 7.4 and he says the first line gives true for him. So the same code gives different answers on different versions? That seems mad. Which rules does == actually follow, and is === really always the right choice, even when I compare a number from a form with a number in my code?

    0
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

  1. Kaveh S.Member ·

    JS has the same problem, == does type juggling and === doesn't. Rule in bootcamp: === unless you have a reason.

  2. Lin W.Member ·

    This bit me on the restaurant site. An order status of "0" in the database matched an empty string check somewhere. I still don't fully understand why, so following this.

CoursesWeb Forum. Questions and answers on learning web development: HTML, CSS, JavaScript, PHP, MySQL, Node.js and AI coding help.