Equal Vs Identical difference in php Example

Equal (==) vs Identical (===):

Equal (==):

To compare the value of the variable.

Eg:

<?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";
}
?>

Output:

Its Equal - string and Integer value are Equal
Its also Equal - Integer and Integer value are equal

Identical (===):

To compare the value of the variable as well as type of the variable

Eg:

<?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";
}
?>

Output:

Its also Equal - Integer and Integer value are equal

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:

491 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *