11 Vote

Difference between two and three equal signs in comparisons

Question by Guest | Last update on 2021-05-04 | Created on 2012-08-11

I recently found a comparison with three equal signs in a script. Otherwise, I have always known only comparisons with two equals signs.

According to my tests, it does not matter whether you take two or three equal signs. So, what is this?

ReplyPositiveNegative
2Best Answer2 Votes

The operator == as well as the operator === are both comparision operators.

  • However, two equal signs only look up, whether the two compared variables have the same values.
  • In comparison, three equal signs on the one hand are checking, whether the two variables have the same values and on the other hand, they also check whether the types of the two variables are identical.

Here is an example:

// 3 variables with identical values
$a = 10;
$b = 10;
$c = 10;
 
// $a and $b of type integer, $c of type double
settype($a, integer);
settype($b, integer);
settype($c, double);
 
// comparison of the variables
if ($a == $b) {}  // true
if ($a == $c) {}  // true
if ($a === $b) {} // true
if ($a === $c) {} // false

We define 3 variables with identical values and then cast the variables on the types Integer or Double. Then, we compare the variables in pairs.

When compared with the two equal signs, every time we get out true. In the three equal sign comparison, however, we get only true if the types are also the same.
Last update on 2021-05-04 | Created on 2012-08-12

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.