Difference between double equal to (==) and triple equal to (===) compression operator in Php


Double equal to (==) and triple equal to (===) both are use as compression operator in Php. But there is big difference between double equal to (==) compression operator and triple equal to (===) compression operator.
Double equal to (==) compression operator can only compare the value but triple equal to (===) compression operator compare the value as well as data type of the value. This will be completely clear while seeing the below example.

Example :-
Comparing two value using double equal to compression operator
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
$a = 10;      // integer value
$b = "10";     // string value
if($a == $b)
{
echo "both values are same";
}
else
{
echo "both values are not same";
}
?>
</body>
</html>

Output of the above code
both values are same
The above example clearly shows that the variable  'b' contain string value but the output shows both the values are same.

Now the same example is performed using the triple equal to compression operator
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
$a = 10;      // integer value
$b = "10";     // string value
if($a === $b)
{
echo "both values are same";
}
else
{
echo "both values are not same";
}
?>
</body>
</html>

Output of the above code
both values are not same
While using the triple equal to compression operator the output shows both the values are not same as it compare the value as well as data type of the value.



No comments: