views:

574

answers:

8

I have been programming in PHP for a while but I still dont understand the difference between == and ===. I know that = is assignment. And == is equals to. So what is the purpose of ===?

+4  A: 

It will check if the datatype is the same as well as the value

if ("21" == 21) // true
if ("21" === 21) // false
Bob Fanger
+3  A: 

=== compares value and type.

Andrew Hare
+2  A: 

== doesn't compare types, === does.

0 == false

evaluates to true but

0 === false

does not

Marc W
+25  A: 

It compares both value and type equality.

 if("45" === 45) //false
 if(45 === 45) //true
 if(0 === false)//false

It has an analog: !== which compares type and value inequality

 if("45" !== 45) //true
 if(45 !== 45) //false
 if(0 !== false)//true

It's especially useful for functions like strpos - which can return 0 validly.

 strpos("hello world", "hello") //0 is the position of "hello"

 //now you try and test if "hello" is in the string...

 if(strpos("hello world", "hello")) 
 //evaluates to false, even though hello is in the string

 if(strpos("hello world", "hello") !== false) 
 //correctly evaluates to true: 0 is not value- and type-equal to false

Here's a good wikipedia table listing other languages that have an analogy to triple-equals.

Tom Ritter
In the case of strpos, that "false"/"true" comments isn't very clear, but I guess you can understand it well enough in context.
luiscubal
Also, due to the results Tom points out, it's nearly always better to use === or !== when evaluating return values. Even if the function doesn't currently return an ambiguous value now, that may change in the future.
Dana the Sane
@luiscubal - I clarified.
Tom Ritter
Good answer I think, but the final line you use in your example gives me the willies. Couldn't/shouldn't it be something like if(strpos("hello world", "hello") >= 0) in a real world situation? (Or something similar...I'm making the assumption that strpos either returns -1, like other languages, or can be caught somehow.) Of course, that would ruin the point of your answer so I understand why it's there, but I'm wondering when people might actually use this === or !== type functionality.
Beska
Beska, strpos returns a boolean false, NOT -1. (which indirectly answers you next question, when people might actually use it)
Alan Storm
A: 

It's a true equality comparison.

"" == False for instance is true.

"" === False is false

Oli
+11  A: 

It is true that === compares both value and type, but there is one case which hasn't been mentioned yet and that is when you compare objects with == and ===.

Given the following code:

class TestClass {
  public $value;

  public function __construct($value) {
    $this->value = $value;
  }
}

$a = new TestClass("a");
$b = new TestClass("a");

var_dump($a == $b);  // true
var_dump($a === $b); // false

In case of objects === compares reference, not type and value (as $a and $b are of both equal type and value).

Thorbjørn Hermansen
+5  A: 

The PHP manual has a couple of very nice tables ("Loose comparisons with ==" and "Strict comparisons with ===") that show what result == and === will give when comparing various variable types.

Chad Birch
A: 

Minimally, === is faster than == because theres no automagic casting/coersion going on, but its so minimal its hardly worth mentioning. (of course, I just mentioned it...)

Justin