Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
In PHP, the double equals (==) and triple equals (===) are comparison operators used to compare values for equality. However, they differ in terms of their behaviour and the level of strictness in the comparison process.
Double Equals (==)
The double equals operator checks for equality between two values, but it performs type coercion if the two values have different data types. This means that PHP will attempt to convert the values to a common type before performing the comparison. Here are some key points about the double equals operator
If the two values have the same type, it behaves like the triple equals operator (strict comparison).
If the two values have different types, PHP will attempt to convert them to a common type. For example, if you compare an integer and a string, PHP will try to convert the string to an integer.
Numeric strings are automatically converted to numbers when compared to integers or floats.
Boolean values are compared as integers (true as 1, false as 0).
Null is considered equal to an empty string, an empty array, or zero.
Example 1
Here's how type coercion works with string and integer comparison ?
<?php
$value1 = 5;
$value2 = "5";
if ($value1 == $value2) {
echo "Equal";
} else {
echo "Not Equal";
}
?>
Equal
The double equals operator performs type coercion, so the string "5" is converted to the integer 5, and the comparison returns true.
Example 2
Type coercion also works with floating point strings ?
<?php
$value1 = 5;
$value2 = "5.0";
if ($value1 == $value2) {
echo "Equal";
} else {
echo "Not Equal";
}
?>
Equal
The string "5.0" is converted to the float 5.0, which then equals the integer 5, so the comparison returns true.
Triple Equals (===)
The triple equals operator, also known as the identity operator, performs a strict comparison between two values. It checks for both value and type equality without performing any type coercion. Here are the key characteristics
It returns true only if the two values being compared have the same type and the same value.
If the types are different, the operator returns false.
It does not perform any type conversion or coercion before the comparison.
It is often preferred when comparing values where type integrity is important.
Example 1
Strict comparison considers both value and type ?
<?php
$value1 = 5;
$value2 = "5";
if ($value1 === $value2) {
echo "Equal";
} else {
echo "Not Equal";
}
?>
Not Equal
Since $value1 is an integer and $value2 is a string, they are not considered equal with strict comparison.
Example 2
When both type and value match, strict comparison returns true ?
<?php
$value1 = 10;
$value2 = 10;
if ($value1 === $value2) {
echo "Equal";
} else {
echo "Not Equal";
}
?>
Equal
Since both $value1 and $value2 are integers with the same value of 10, the strict comparison returns true.
Comparison Table
| Operator | Type Coercion | Checks Type | Example Result |
|---|---|---|---|
| == | Yes | No | 5 == "5" ? true |
| === | No | Yes | 5 === "5" ? false |
Conclusion
The double equals (==) performs loose equality with type coercion, while triple equals (===) performs strict equality without type conversion. Use === when you need precise type and value matching, as it prevents unexpected behavior from automatic type conversions.
