Equal Vs Identical difference in php Example

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:
[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
$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:
[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:

880 total views, 2 views today

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.