-5

Possible Duplicate:
php == vs === operator
Is there a difference between !== and != in PHP?

In PHP, the condition of the if command contains operator === and !==.

I never use them. So I wonder when will we actually need to call them?

E.g.

if (FALSE !=  someMethod() ) {
}

if (FALSE !== someMethod() ) {
}

What may go wrong with the 1st if?

Community
  • 1
  • 1
Nam G VU
  • 33,193
  • 69
  • 233
  • 372

3 Answers3

4
0 == '' == null == false == array()

If you need to know the difference between two of these, you need ===.

http://php.net/manual/en/function.strpos.php

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

strpos('apple', 'a') == 0

strpos('apple', 'b') == false

Without a === you won't know if 'apple' has 'a' in the first position or if it doesn't exist.

evan
  • 12,307
  • 7
  • 37
  • 51
  • For strpos sample, I don't think comparing with boolean value is the right usage of this method. – Nam G VU Jan 08 '16 at 04:51
  • Though, your statement on `0`, `null`, `''`, `false`, and `array()` is the excellent point-out for me. Great thanks. – Nam G VU Jan 08 '16 at 04:52
3

Take an example like this:

$str = '*Hello*World*';
if (FALSE != strpos($str, '*')){
    // Echo if string has an '*' in it!
    echo $str;
}

This won't work, because strpos() returns the index of the first match. In this case it returns 0. FALSE == 0, but FALSE !== 0.

Nam G VU
  • 33,193
  • 69
  • 233
  • 372
Paul
  • 139,544
  • 27
  • 275
  • 264
  • I think your good scenario describe a wrong usage of `strpos` function - the meaning of the returned integer value SHOULD NOT be used as boolean at all. And that's not persuade enough for me to use !== there indeed. – Nam G VU Jan 08 '16 at 04:49
  • 1
    @NamGVU `strpos` returns a boolean if there is no match. It can also return `0` if the match is at the very beginning of the string. If you use `!=` you can't tell whether it returned `0` or `false`, but if you use `!==` you can tell the difference between a match at the beginning of a string or no match at all. – Paul Jan 08 '16 at 05:00
  • 1
    @NamGVU Also see the warning in the section about the return values in the PHP manual page for [strpos](http://php.net/manual/en/function.strpos.php) – Paul Jan 08 '16 at 05:01
  • Agree. My bad not reading the manual carefully. Thanks a lot for helping me to know that! – Nam G VU Jan 08 '16 at 09:10
  • After all, this leads to PHP's design for such `strpos` function - I mean if PHP considers FALSE and 0 to be "the same" when compared, then they should make strpos to be the traditional way as other languages do e.g. C++, Java, C# ect. – Nam G VU Jan 08 '16 at 09:11
  • @NamGVU Yeah, PHP is a very weird language in many different ways. – Paul Jan 08 '16 at 15:28
0

PHP uses loose comparison when you use == and !=' instead of===and '!==. Loose comparison means, that types are compared on 'similarity'. To get an overview of how types compare, look here

bigblind
  • 12,539
  • 14
  • 68
  • 123