views:

2322

answers:

5

Can you explain the difference between == and ===, giving some useful examples?

+42  A: 

== compares the values of variables for equality, type casting as necessary. === checks if the two variables are of the same type AND have the same value.

A full explanation of the differences are available in the PHP manual.

Here's a table I put together showing how some variables compare to each other.

// "===" means that they are identical  
// "==" means that they are equal  
// "!=" means that they aren't equal.

         false    null     array()  0        "0"      0x0      "0x0"    "000"    "0000"
false    ===      ==       ==       ==       ==       ==       !=       !=       !=    
null     ==       ===      ==       ==       !=       ==       !=       !=       !=    
array()  ==       ==       ===      !=       !=       !=       !=       !=       !=    
0        ==       ==       !=       ===      ==       ===      ==       ==       ==    
"0"      ==       !=       !=       ==       ===      ==       ==       ==       ==    
0x0      ==       ==       !=       ===      ==       ===      ==       ==       ==    
"0x0"    !=       !=       !=       ==       ==       ==       ===      ==       ==    
"000"    !=       !=       !=       ==       ==       ==       ==       ===      ==    
"0000"   !=       !=       !=       ==       ==       ==       ==       ==       ===
nickf
This works much the same in Javascript, btw.
Raithlin
thanks - i've added that to the tags now.
nickf
does anyone else find it strange that "000" == "0000" ?
nickf
Maybe strings that look like numbers are converted to numbers before comparison, if that's true, then 000 == 0000 makes sense.
Kevin
What always suprises me is that false == array(), and false == 0 but array() != 0, so false == array() !=/== 0? that feels weird to me.
Pim Jager
@Pim Well, there are a few more cases like that, like `null != "0"`. In the end, you *are* comparing apples to oranges so to speak, so it's okay. "0" really isn't `null` after all. They just all happen to cast to BOOLs very nicely, which is what you usually want to compare them to.
deceze
@Pim ...continued: Look at it this way: Casting to a BOOL, any value only has to fall on one of two sides, `true` or `false`. That's easy to cast. All other values though have, for all practical purposes, virtually unlimited combinations. Is `"five" == 5`? `array(0) == 0`? `array(0,0,0) == 0`? `0.0000000000000000000000000000000000000000000000000001 == array()`?
deceze
A: 

You would use === to test whether a function or variable is false rather than just equating to false (zero or an empty string).

$needle = 'a';
$haystack = 'abc';
$pos = strpos($haystack, $needle);
if ($pos === false) {
    echo $needle . ' was not found in ' . $haystack;
} else {
    echo $needle . ' was found in ' . $haystack . ' at location ' . $pos;
}

In this case strpos would return 0 which would equate to false in the test

if ($pos == false)

or

if (!$pos)

which is not what you want here.

Stacey Richards
+13  A: 

In regards to Javascript

The === operator works the same as the == operator but requires that its operands have not only the same value, but also the same data type.

For example the sample below will display 'x and y are equal' but not 'x and y are identical'

var x = 4;
var y = '4';
if (x == y) {
    alert('x and y are equal');
}
if (x === y) {
    alert('x and y are identical');
}
Michael Wolfenden
Upvoted, as this seems to be exactly the same situation for php.
David Thomas
A: 

Variables have a type and a value.

  • $var = "test" is a string that contain "test"
  • $var2 = 24 is an integer vhose value is 24.

When you use these variables (in PHP), sometimes you don't have the good type. For example, if you do

if ($var == 1) {... do something ...}

PHP have to convert ("to cast") $var to integer. In this case, "$var == 1" is true because any non-empty string is casted to 1.

When using ===, you check that the value AND THE TYPE are equal, so "$var === 1" is false.

This is useful, for example, when you have a function that can return false (on error) and 0 (result) :

if(myFunction() == false) { ... error on myFunction ... }

This code is wrong as if myFunction() returns 0, it is casted to false and you seem to have an error. The correct code is :

if(myFunction() === false) { ... error on myFunction ... }

because the test is that the return value "is a boolean and is false" and not "can be casted to false".

ofaurax
regarding non-empty strings, that's actually not true. "a" == 0 is TRUE.
nickf
A: 

Given x = 5

1) Operator : == is "equal to". x == 8 is false
2) Operator : === is "exactly equal to" (value and type) x === 5 is true, x === "5" is false

Mannusanghi