Friday 17 June 2016

Difference between double and triple equal to operators in PHP

This is one of the most common interview questions in PHP. Both == and === are comparison operators which are used to compare two values. But their is a major difference between the two. The ‘==’ (double equal to) compares and test if the two values on left and right side are equal. On the other hand, ‘===’ (triple equal to) tests if the two values are equal as well as check if they are of same data type. Let me explain you with help of an example,
<?php
$x = 100; $y = "100";
if($x == $y){
 echo "Yes";
}else{
 echo "No";
}
?>
The output of above code will be “YES” as  “==” operator compares only values and values in $x and $y are same.
<?php
$x = 100; $y = "100";
if($x === $y){
 echo "Yes";
}else{
 echo "No";
}
?>
The output of above code will be “NO” as even the values in $x and $y are same, there data type is different.

0 comments:

Post a Comment