Home  >  Article  >  Backend Development  >  What is the difference between PHP's equality (==double equal sign) and identity (===triple equal sign) comparison operators?

What is the difference between PHP's equality (==double equal sign) and identity (===triple equal sign) comparison operators?

WBOY
WBOYforward
2023-09-07 17:45:081397browse

What is the difference between PHPs equality (==double equal sign) and identity (===triple equal sign) comparison operators?

In PHP, double equals (==) and triple equals (===) are comparison operators used to compare values ​​for equality. However, they differ in their behavior and the rigor of the comparison process.

Double equals (==)

The double equals operator checks for equality between two values, but performs type coercion if the two values ​​have different data types. This means that PHP will try to convert the value to a generic type before performing the comparison. Here are some key points about the double equals operator:

  • If two values ​​have the same type, it behaves like the triple equals operator (strict comparison).

  • If two values ​​have different types, PHP will try 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.

  • In contrast to integers or floating point numbers, numeric strings are automatically converted to numbers.

  • Boolean values ​​are compared as integers (true is 1, false is 0).

  • Null is treated as equal to the empty string, empty array, or zero.

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

Example 1

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


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

Output

Equal

illustrate

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

illustrate

The double equals operator performs type coercion. The string "5.0" is converted to the floating point number 5.0, and the comparison returns true.

Triple equal sign (===)

The triple equality operator, also known as the identity operator, performs a strict comparison between two values. It checks value and type equality without performing any type coercion. The triple equals operator is more strict and is generally considered safer to use because it avoids unexpected or unexpected 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.

  • The operator returns false if the types are different, or if one value cannot be cast to the other value's type.

  • No type conversion or cast is performed before comparison.

  • It is usually 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

illustrate

The triple equals operator performs a strict comparison, taking into account 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

illustrate

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

in conclusion

In PHP, the comparison behavior of the double equals (==) and triple equals (===) operators is different. Double equals enforces a loose equality check by type, attempting to convert the value to a common type before comparing. In contrast, triple equals does a strict equality check without type coercion, taking into account both value and type. The triple sign is often favored for its reliability and predictability. It helps prevent unexpected behavior caused by unintentional type conversions. Understanding these differences enables developers to choose the appropriate operator based on the desired comparison requirements and maintain code accuracy.

The above is the detailed content of What is the difference between PHP's equality (==double equal sign) and identity (===triple equal sign) comparison operators?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:fscanf() function in PHPNext article:fscanf() function in PHP