Question
In PHP, what is the difference between the == equality operator and the === identity operator?
How does loose comparison with == work, and how does strict comparison with === work?
Please include practical examples that show when the results are different.
$a = 5;
$b = "5";
var_dump($a == $b); // ?
var_dump($a === $b); // ?
Short Answer
By the end of this page, you will understand how PHP compares values using == and ===, why type coercion matters, and why === is usually safer in real code. You will also see examples involving numbers, strings, booleans, null, and arrays so you can predict comparison results more confidently.
Concept
In PHP, == and === both compare two values, but they do not behave the same way.
==checks whether two values are equal after PHP converts types if needed.===checks whether two values are identical, meaning both the value and the type must already match.
This difference matters because PHP is a loosely typed language. A variable can hold different types of values, and PHP may automatically convert one type to another during some operations. That automatic conversion is called type juggling.
Loose comparison: ==
When you use ==, PHP tries to make the two operands comparable. That can involve converting:
- strings to numbers
- booleans to integers
nullto other empty-like values in some cases
This can produce results that look surprising at first.
var_dump(5 == "5"); // true
var_dump(false == 0); // true
var_dump(null == );
Mental Model
Think of == as saying:
"These two things look the same after I normalize them."
Think of === as saying:
"These two things are exactly the same item type and value."
A simple analogy:
==is like comparing two addresses after removing spaces, punctuation, and formatting differences.===is like checking whether the addresses are character-for-character identical.
Another analogy with boxes:
- Every value is stored in a box with a label (its type) and contents (its value).
==mostly compares the contents, even if the labels differ.===compares both the label and the contents.
So for PHP:
5 == "5" // same content after conversion
5 === "5" // different labels: int vs string
Syntax and Examples
Core syntax
$value1 == $value2 // loose comparison
$value1 === $value2 // strict comparison
Basic example
$a = 5;
$b = "5";
var_dump($a == $b); // true
var_dump($a === $b); // false
Why?
$ais an integer:5$bis a string:"5"- With
==, PHP converts types and compares the values. - With
===, PHP checks both type and value, sointis not the same asstring.
More examples
Numbers and strings
Step by Step Execution
Consider this example:
$a = 0;
$b = false;
var_dump($a == $b);
var_dump($a === $b);
Step-by-step for ==
1. PHP sees different types
$ais an integer:0$bis a boolean:false
2. PHP performs type juggling
For loose comparison, PHP converts values to make them comparable.
3. Comparison result
0andfalseare treated as equal in loose comparison.
var_dump($a == $b); // true
Step-by-step for ===
Real World Use Cases
1. Validating form input
Form values usually arrive as strings.
$age = $_POST['age']; // "18"
if ($age == 18) {
echo "Loose match";
}
if ($age === 18) {
echo "Strict match";
}
Here, the first condition is true, but the second is false because form input is a string unless you convert it.
2. Checking function return values
Some PHP functions return either a normal value or false.
$index = array_search("apple", ["apple", "banana"]);
if ($index === false) {
echo "Not found";
} else {
echo "Found at index $index";
}
This is safer than because index is a valid result.
Real Codebase Usage
In real PHP projects, developers usually follow one simple rule:
- Use
===and!==by default. - Use
==only when you intentionally want type juggling.
Common patterns
Guard clauses
if ($user === null) {
return;
}
This makes it clear that only null should trigger the early return.
Validation
if (!is_int($age)) {
throw new InvalidArgumentException('Age must be an integer.');
}
After validating types, strict comparisons become reliable.
Function result checks
$pos = strpos($text, $needle);
if ($pos === false) {
}
Common Mistakes
1. Using == false with functions that can return 0
Broken code:
$pos = strpos("hello", "h");
if ($pos == false) {
echo "Not found";
}
Why it is wrong:
strpos()returns0when the match is at the first character.0 == falseistrue.
Correct code:
if ($pos === false) {
echo "Not found";
}
2. Assuming form input numbers are actual integers
Broken code:
$quantity = $_POST['quantity']; // "3"
if ( === ) {
;
}
Comparisons
== vs ===
| Operator | Name | Compares value | Compares type | Type conversion | Common use |
|---|---|---|---|---|---|
== | Equality | Yes | No | Yes | Rare, when loose matching is intentional |
=== | Identity | Yes | Yes | No | Default choice in most PHP code |
Example outcomes
| Expression | Result | Why |
|---|
Cheat Sheet
Quick rules
==means equal after possible type conversion===means same type and same value- Prefer
===in most code - Convert input first, then compare strictly
Syntax
$a == $b
$a === $b
$a != $b
$a !== $b
Common examples
5 == "5" // true
5 === "5" // false
false == 0 // true
false === 0 // false
null == false // true
null === false // false
Safe pattern
= () [];
( === ) {
;
}
FAQ
What is the difference between == and === in PHP?
== compares values loosely and may convert types first. === compares both value and type without conversion.
Should I use == or === in PHP?
In most cases, use ===. It is safer, clearer, and avoids bugs caused by automatic type conversion.
Why does 5 == "5" return true in PHP?
Because == performs loose comparison. PHP converts the string "5" to the number 5, then compares them.
Why does 5 === "5" return false?
Because === requires both operands to have the same type and value. One is an integer and the other is a string.
Why is strpos() often checked with === false?
Because strpos() can return for a valid match at the beginning of a string. Using would incorrectly treat as .
Mini Project
Description
Build a small PHP script that checks values entered from different sources and shows the difference between loose and strict comparison. This project demonstrates how real input often arrives as strings and why strict comparison becomes more reliable after normalization.
Goal
Create a PHP script that compares several pairs of values using both == and === and prints the results clearly.
Requirements
- Create at least four pairs of values with different types, such as
5and"5",0andfalse,nullandfalse, and two arrays. - For each pair, print the value types and the results of both
==and===. - Include one example using
strpos()orarray_search()to show why=== falseis important. - Add one example where you cast a string input to an integer before using
===.
Keep learning
Related questions
Are PDO Prepared Statements Enough to Prevent SQL Injection in PHP?
Learn how PDO prepared statements prevent SQL injection in PHP, what they protect, and the mistakes that still leave MySQL apps vulnerable.
Can You Bind an Array to an IN Clause in PHP PDO?
Learn how PDO handles placeholders in IN() clauses, why arrays cannot be bound directly, and the safe PHP pattern to build dynamic queries.
Choosing the Right MySQL Collation for PHP and UTF-8
Learn how MySQL character sets and collations work with PHP, and how to choose a practical UTF-8 setup for web applications.