5

Possible Duplicates:
How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?
Reference - What does this symbol mean in PHP?
php not equal to != and !==

What are the !== and === operators in this code snippet?

if ( $a !== null ) // do something
if ( $b === $a ) // do something
Community
  • 1
  • 1
xRobot
  • 25,579
  • 69
  • 184
  • 304
  • 8
    http://www.php.net/manual/en/language.operators.comparison.php – mbeckish Nov 02 '10 at 17:30
  • 8
    It's really unfortunate that the SO search engine doesn't work for '===' because this question gets asked about once a month. – Paul Tomblin Nov 02 '10 at 17:31
  • One of the best answers is provided as comment xD – Álvaro González Nov 02 '10 at 17:31
  • 1
    @Paul There's [this](http://stackoverflow.com/questions/80646/how-do-the-equality-double-equals-and-identity-triple-equals-compariso) for `===` and [this](http://stackoverflow.com/questions/3641819/php-not-equal-to-and) for `!==`. This is kind of a duplicate of both; I'm voting to close as a duplicate of the first, and hopefully somebody else votes for the second so they both show up in the list (whoever voted to close as NaRQ is just lazy) – Michael Mrozek Nov 02 '10 at 17:32
  • 1
    @Paul Tomblin: You must come here pretty infrequently ;) – BoltClock Nov 02 '10 at 17:34
  • Here is another one: [What does “===” mean?](http://stackoverflow.com/questions/1117967/what-does-mean) – Felix Kling Nov 02 '10 at 17:35
  • @Bolt, maybe I'm guilty of under-estimation. I'm not really active in the php tag. – Paul Tomblin Nov 02 '10 at 17:46
  • @Paul: \*checks your tags\* You're right. – BoltClock Nov 02 '10 at 17:47

6 Answers6

13

They are identity equivalence operators.

1 == 1
1 == "1"
1 === 1
1 !== "1"
true === true
true !== "true"
true == "true"

All of these equate to true. Also check this link provided by @mbeckish

Gabi Purcaru
  • 30,940
  • 9
  • 79
  • 95
  • 2
    I wouldn't call it identity though, because two objects with a different identity can still have the same value and the same type (and be `===` equal as such). There is just no *identity* in php. – poke Nov 02 '10 at 17:35
  • It's the equivalence operator. It asserts that both "arguments" (left and right side) are both equal and of the same type (and if an object, the same class). – ircmaxell Nov 02 '10 at 17:44
6

They are strict type comparison operators. They not only check the value but also the type.

Consider a situation when you compare numbers or strings:

if (4 === 4) // same value and type
{
  // true
}

but

if (4 == "4") // same value and different type but == used
{
  // true
}

and

if (4 === "4") // same value but different type
{
  // false
}

This applies to objects as well as arrays.

So in above cases, you have to make sensible choice whether to use == or ===

It is good idea to use === when you are sure about the type as well

More Info:

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
1

=== also checks for the type of the variable.

For instance, "1" == 1 returns true but "1" === 1 returns false. It's particularly useful for fonctions that may return 0 or False (strpos for instance).

This wouldn't work correctly because strpos returns 0 and 0 == false

if (strpos('hello', 'hello world!'))

This, however, would work :

if (strpos('hello', 'hello world!') !== false)
Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
  • This answer is correct, but I am going to add an example. `strpos("abcde", "f")` returns `false`, because `f` is not in the string `abcde`. You may want to test for this, so you would use `if (strpos("abcde", "f")===false)`. If you simply did `if(!(strpos("abcde", "f")))` then you would be in trouble if strpos actually returned `0`. Why? Because PHP is loosely typed and `0`0 and `false` are equivalent when not comparing types as well. – Brad Nov 02 '10 at 17:38
0

A double = sign is a comparison and tests whether the variable / expression / constant to the left has the same value as the variable / expression / constant to the right.

A triple = sign is a comparison to see whether two variables / expresions / constants are equal AND have the same type - i.e. both are strings or both are integers.

The same concept applies for !==

Martin
  • 10,294
  • 11
  • 63
  • 83
0

They will only return true if both the type and value of the values given are equivalent. Example: 1 === 1 is true "1" === 1 is false 1 === "1" is false "1" === "1" is true

where as with == all of the above would be true

0

When you use two equal signs == it will check for the same value.

if( '1' == 1 ) { echo 'yes'; }

The above code works because they have the same value.

But if you use three equal signs === it will check the value and the data type.

Therefore

if( '1' === 1 ) { /* this will not work */ }

This is because '1' has a data type of string while 1 is an integer or a number

But you could do something like this - I think :D

if( (integer) '1' === 1 ) { echo 'this works'; }

Because we are changing the data type of '1' to an integer

Baylor Rae'
  • 3,995
  • 20
  • 39