Equal Vs Identical difference in php Example
Equal (==) vs Identical (===):
Equal (==):
To compare the value of the variable.
Eg:
[php gutter=”false”]
<?php
$a = 10;
$b = 10;
if("$a" == $b){
echo "Its Equal – string and Integer value are Equal\n";
}
if($a == $b){
echo "Its also Equal – Integer and Integer value are equal";
}
?>
[/php]
Output:
[plain gutter=”false”]
Its Equal – string and Integer value are Equal
Its also Equal – Integer and Integer value are equal
[/plain]
Identical (===):
To compare the value of the variable as well as type of the variable
Eg:
[php gutter=”false”]
<?php
$a = 10;
$b = 10;
if("$a" === $b){
echo "Its Equal – string and Integer value are Equal\n";
}
if($a === $b){
echo "Its also Equal – Integer and Integer value are equal";
}
?>
[/php]
Output:
[plain gutter=”false”]
Its also Equal – Integer and Integer value are equal
[/plain]
Important:
1. If you compare both integer and string variable also, if both variable have same value then equal will return true.
2. If you compare integer and integer with same value and same variable type only, identical will return true.
PHP Recommended Books: