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.

  • Arrays and objects are not compared by their contents but by their identity.

Example 1

<?php
$value1 = 5;
$value2 = "5";


if ($value1 == $value2) {
   echo "Equal";
} else {
   echo "Not Equal";
}
?>

Output

Equal

Explanation

The double equals operator performs type coercion, so the string "5" is converted to the integer 5, and the comparison returns true.

Example 2

<?php
$value1 = 5;

$value2 = "5.0";

if ($value1 == $value2) {
   echo "Equal";
} else {
   echo "Not Equal";
}
?>

Output

Equal

Explanation

The double equals operator performs type coercion. The string "5.0" is converted to the float 5.0, and 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. The triple equals operator is more strict and generally considered safer to use because it avoids unexpected or unintended type conversions. Here are some key points about the triple equals operator:

  • It returns true only if the two values being compared have the same type and the same value.

  • If the types are different, or if one value is not coercible to the type of the other value, 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

<?php
$value1 = 5;
$value2 = "5";

if ($value1 === $value2) {
   echo "Equal";
} 
else {
   echo "Not Equal";
}
?>

Output

Not Equal

Explanation

The triple equals operator performs a strict comparison, considering both value and type. Since $value1 is an integer and $value2 is a string, they are not considered equal.

Example 2

<?php
$value1 = 10;
$value2 = 10;

if ($value1 === $value2) {
   echo "Equal";
} else {
   echo "Not Equal";
}
?>

Output

Equal

Explanation

Since both $value1 and $value2 are integers with the same value of 10, the strict comparison using === returns true, and "Equal" is echoed as the output.

Conclusion

In PHP, the double equals (==) and triple equals (===) operators differ in their comparison behaviour. The double equals performs loose equality checks with type coercion, attempting to convert values to a common type before comparison. In contrast, the triple equals conducts strict equality checks without type coercion, considering both value and type. The triple equals is generally preferred for its reliability and predictability. It helps prevent unexpected behaviour caused by unintentional type conversions. Understanding the distinctions empowers developers to choose the appropriate operator based on the desired comparison requirements and maintain code accuracy.

Updated on: 28-Jul-2023

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements