Introduction: The Trap All Beginners Fall Into
\n\nImagine this: you're writing your first PHP script, checking if a user entered the correct password, and you use the condition if ($password == "secret"). Everything works perfectly. But a month later, you notice strange behavior: if (0 == "off") returns true, even though these are two completely different things. Sound familiar?
Welcome to the world of PHP comparison operators. Here, double equals (==) and triple equals (===) are not just different symbols, but two completely different approaches to comparison. If you don't understand the difference, your code can behave unpredictably, and bugs will hide where you least expect them.
\n\nIn this article, we'll cover:
\n- \n
- How each operator works "under the hood" \n
- Why
"123abc" == 123is true, but"123abc" === 123is false \n - What pitfalls await you (spoiler: there are many) \n
- When to use which operator in real projects \n
Ready? Let's go!
\n\n1. Basics: What Are == and === in PHP?
\n\nBoth operators are used to compare two values. But their approach is fundamentally different.
\n\nThe Equality Operator (==) — "Type Casting Allowed"
\nDouble equals (or loose comparison) first converts both operands to the same type, then compares their values. If the types are different, PHP automatically casts them to a common denominator. This is convenient, but dangerous.
\n\nThe Identity Operator (===) — "Everything Must Match"
\nTriple equals (or strict comparison) compares both values and data types. If the types are different — immediately false, without any conversions.
\n\nHere's a simple table to remember:
\n- \n
$a == $b— Are the values equal after type casting? \n $a === $b— Are the values equal and do the types match? \n
2. Simple Example: Numbers and Strings
\n\nLet's look at a classic case — comparing a number and a string.
\n\n<?php\n$a = 5; // integer\n$b = "5"; // string\n\n// Loose comparison (==)\nvar_dump($a == $b); // bool(true) — PHP converts the string "5" to the number 5\n\n// Strict comparison (===)\nvar_dump($a === $b); // bool(false) — types are different: integer vs string\n?>\n\nIn the first case, PHP thinks: "Aha, a number and a string. The string "5" can easily be turned into the number 5, so they are equal." In the second case: "No, integer is not string, immediately false."
\n\nIt seems like a small thing. But this is where the problems begin.
\n\n3. The Main Pitfall: "Emptiness" and "Zero"
\n\nThe most common mistake beginners make is confusion with falsy values. In PHP, there are several values that, under loose comparison, are considered equal to each other, even though they are completely different in meaning.
\n\nLook at this code:
\n\n<?php\nvar_dump(0 == false); // bool(true) — zero equals false\nvar_dump(0 == null); // bool(true) — zero equals null\nvar_dump(false == null); // bool(true) — false equals null\nvar_dump("" == false); // bool(true) — empty string equals false\nvar_dump("" == 0); // bool(true) — empty string equals zero\n?>\n\nShocked? You bet! 0 == false is true, "" == 0 is also true. If you're checking whether a user entered a password and write if ($password == ""), then compare it with if ($password == 0) — both conditions can trigger the same way, even though they are different scenarios.
Now compare with triple equals:
\n\n<?php\nvar_dump(0 === false); // bool(false)\nvar_dump(0 === null); // bool(false)\nvar_dump("" === false); // bool(false)\nvar_dump("" === 0); // bool(false)\n?>