Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Difference between == and === operator in PHP

 In PHP == is equal operator and returns TRUE if $a is equal to $b after type juggling.

And === is an Identical operator and returns TRUE if $a is equal to $b, and they are of the same data type.

Two of the many comparison operators used by PHP are ‘==’ (i.e. equal) and ‘===’ (i.e. identical). The difference between the two is that ‘==’ should be used to check if the values of the two operands are equal or not. On the other hand, ‘===’ checks the values as well as the type of operands.

Let me explain more using some examples:

== (equal):

<?php
if(“22” == 22) {
echo “Equal”;
} else {
echo “Not Equal”;
}
?>

Output: Equal
The code above will print “Equal”. The reason is that the values of the operands are equal.

Whereas when we run the example code below:
=== (Identical):

<?php
if(“22” === 22) {
echo “Equal”;
} else {
echo “Not Equal”;
}
?>

Output: Not Equal
The result we get is “Not Equal”. The reason is that although values of both operands are same their types are different, “22” (with quotes) is a string while 22 (w/o quotes) is an integer.

<?php 
   $a=true ;
   $b=1;
   // Below condition returns true and prints a and b are equal
   if($a==$b){
    echo "a and b are equal";
   }else{
    echo "a and b are not equal";
   }
   //Below condition returns false and prints a and b are not equal because $a and $b are of  different data types.
   if($a===$b){
    echo "a and b are equal";
   }else{
    echo "a and b are not equal";
   }
?>  

If you have any query or suggestions, feel free to ask me via the comment section below.

must watch some other topics :

cake PHP training institute
cake PHP training course
cake php training online

Post a Comment

0 Comments