6

Possible Duplicate:
How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?

I know the basic difference between == and === , but can some experienced coders tell me some practical examples for both cases?

Community
  • 1
  • 1
Vamsi Krishna B
  • 11,377
  • 15
  • 68
  • 94
  • 4
    See http://stackoverflow.com/questions/3641819/php-not-equal-to-and/3641837#3641837 (compares `!=` and `!==` but you get the idea) – BoltClock Jan 19 '11 at 07:24
  • http://www.php.net/manual/en/types.comparisons.php gives a great overview! – Paul Jan 19 '11 at 14:14

3 Answers3

23

== checks if the values of the two operands are equal or not. === checks the values as well as the type of the two operands.

if("1" == 1)
   echo "true";
else
   echo "false";

The above would output true.

if("1" === 1)
   echo "true";
else
   echo "false";

The above would output false.

if("1" === (string)1)
   echo "true";
else
   echo "false";

The above would output true.

David Titarenco
  • 32,662
  • 13
  • 66
  • 111
1

Easiest way to display it is with using strings. Two examples:

echo ("007" === "7" ? "EQUAL!" : "not equal"); 
echo ("007" == "7" ? "EQUAL!" : "not equal"); 
Bjoern
  • 15,934
  • 4
  • 43
  • 48
1

In addition to @DavidT.'s example, a more practical example is the following:

$foo = "Goo";
$bar = "Good Morning";
if (strpos($bar,$foo))
  echo "Won't be seen, returns false because the result is in fact 0";
if (strpos($bar,$foo) !== false)
  echo "True, though 0 is returned it IS NOT false)";
Brad Christie
  • 100,477
  • 16
  • 156
  • 200