Double Equal and Triple Equal in PHP with Difference

Most of the PHP learners are in doubt on double equal and triple equal in PHP. Moreover, they are not sure where to use double equal and where to use triple equal in PHP.

So, In this tutorial, I will cover up these following topics.

  1. Where should I use double equal in PHP
  2. Where should I use triple equal in PHP
  3. Difference between double equal and triple equal in PHP

Double Equal in PHP and How it works

Double equal == is basically known as an equal operator in PHP. Remeber, assignment operator = and equal operator == are not same in PHP.

The assignment operator assigned the value of the left variable to a new value, which is kept on the right side of the assignment operator. Whereas, an Equal operator in PHP returns either true or false. If both sides of the equal operator match each other it returns an out true else it returns false.



So just look at the below Double equal example in PHP

<?php
  $a=5;
  $b=5;
  if($a==$b){
    echo "equal";

  }
  else{
    echo "not equal";
  }


?>

This will output: equal

Now take another example,

 

<?php
  $a="5";
  $b=5;
  if($a==$b){
    echo "equal";

  }
  else{
    echo "not equal";
  }


?>

This time we used double quote around 5, this means $a is now going to be treated as a string.

But this time we will also get the same output: equal

As an equal operator checks the value not the type of the data. So here it will return true, thus it satisfies the if statement and display equal as output.

Triple equal in PHP ( Identical Operator )

Identical Operator or triple equal in PHP also compare the value of both sides of the identical operator. But in this case, it is more strict. Because it checks the value as well as the type of the data.

For easy understanding take a look at the below code.

<?php
  $a="5";
  $b=5;
  if($a===$b){
    echo "equal";

  }
  else{
    echo "not equal";
  }


?> 

This will output: not equal

Because here the value of $a and $b is the same but these are of the different data type as we took the double quotes to assign the value of $a.

Also Read,
Echo and Print Statement Difference in PHP

How To Print Variable Name With Dollar Sign In PHP

Difference between double equal and triple equal in PHP

Both the operators can be used to compare the value. But in the case of the triple equal operator, it also compares the type of the data.

Double equal returns true if the values are the same and don’t care about the type of the data. But the triple equal in PHP also checks the type of the data as well as the value.

Leave a Reply

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