PHP == and ===: What's the Difference and Why Does It Matter?

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: The Trap All Beginners Fall Into

\n\n

Imagine 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?

\n\n

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\n

In this article, we'll cover:

\n
    \n
  • How each operator works "under the hood"
  • \n
  • Why "123abc" == 123 is true, but "123abc" === 123 is false
  • \n
  • What pitfalls await you (spoiler: there are many)
  • \n
  • When to use which operator in real projects
  • \n
\n\n

Ready? Let's go!

\n\n

1. Basics: What Are == and === in PHP?

\n\n

Both operators are used to compare two values. But their approach is fundamentally different.

\n\n

The Equality Operator (==) — "Type Casting Allowed"

\n

Double 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\n

The Identity Operator (===) — "Everything Must Match"

\n

Triple equals (or strict comparison) compares both values and data types. If the types are different — immediately false, without any conversions.

\n\n

Here'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
\n\n

2. Simple Example: Numbers and Strings

\n\n

Let'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\n

In 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\n

It seems like a small thing. But this is where the problems begin.

\n\n

3. The Main Pitfall: "Emptiness" and "Zero"

\n\n

The 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\n

Look 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\n

Shocked? 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.

\n\n

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?>

Blogs

Book Recommendations