3

Possible Duplicate:
php == vs === operator

I understand the difference between ===, !== vs. ==, != in PHP but I always find myself favouring === and !== but I have no real reason why. In the documentation on PHP.net, there are often 'warnings' saying that you should use === to check the return value for some built-in functions. So my question is why should I use the one set of operators over another?

Many thanks :).

Community
  • 1
  • 1
ale
  • 11,636
  • 27
  • 92
  • 149
  • Use them when they are appropriate. If you understand the difference, what is the question? Used up all my close votes, but this is a quite non-constructive question. – kapa May 08 '12 at 09:18
  • Are there more appropriate sites to ask this sort of question on then? There is programmers.stackexchange? – ale May 08 '12 at 09:23
  • 2
    I am not quite familiar with the FAQ of `programmers.SE`. My problem with your question is that it `will likely solicit opinion, debate, arguments, polling, or extended discussion`. Nothing personal. Both `===` and `==` have their places, so you cannot really say 'use one over the other' **generally**. – kapa May 08 '12 at 09:31
  • I understand, I have voted to close. – ale May 08 '12 at 09:47
  • possible duplicate of [php == vs === operator](http://stackoverflow.com/questions/589549/php-vs-operator) and [How do the equality (== double equals) and identity (=== triple equals) comparison operators differ](http://stackoverflow.com/questions/80646/how-do-the-equality-double-equals-and-identity-triple-equals-comparis) – Salman A May 08 '12 at 10:23

4 Answers4

5

Automatic type conversion can lead to unexpected false positives and false negatives if it doesn't convert the way you (or the developer who has to maintain your code in the future) expect it (and the rules for type conversation are usually complex and vary between languages, so there is plenty of scope for such confusion).

Always use the three character version unless you need type conversation.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
3

=== and !== should be used only in cases if you care that both operands are of the same type.

Otherwise - use == and != - because implicit casting is what we love php for

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 4
    @ThiefMaster: I do love it, because otherwise programming would become a hell of continuous castings, because php gets the data as a strings from all the 3rd party datasources. PS: I always know what type the variable is, so probable implicit casting is not an issue at all – zerkms May 08 '12 at 09:19
  • 2
    @ThiefMaster: it is not horrible, you're overdramatizing :-) PS: I prefer `if ($string)` to `if (strlen($string) > 0)` because it makes a lot of sense for me. Which would **you** choose? – zerkms May 08 '12 at 09:21
  • @Thief `false == 'false'` is `false`. `!'false' === true` is also `false`. – deceze May 08 '12 at 09:21
  • 2
    These types of questions always lead to this same conversation :). – kapa May 08 '12 at 09:22
  • 1
    @baž Indeed. If you're using a dynamically typed language, know its rules and use them to your advantage (less code). Otherwise, use a strongly typed language. :o) – deceze May 08 '12 at 09:23
  • @deceze: hehe, but no one forces you to write confusing code. There are zillion of places perfectly valid for implicit casting. Another is true as well: strictly typed languages don't protect from writing spaghetti ;-) – zerkms May 08 '12 at 09:24
  • 1
    @deceze: Hey, go to another chat to find bugs - we're discussing about types! ;-) – zerkms May 08 '12 at 09:26
  • Ooops, shell quoting issue. damnit. – ThiefMaster May 08 '12 at 09:32
3

The most common reason is then a function returns number / string, and may return false on error. If 0 is valid result, and false indicates error, you have to use === to make the distinction. However, always using === can lead to unexpected behavior, when for example null is returned and is considred also non-valid value, but the type check is for false only. So if data is comming from your own source code / PHP functions and you know that the invalid value is exactly one, use strict check. In most cases, == is a safe choice however, especially if you deal with external data, that is out of your control.

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
1

=== and !== is important if you use a function which may return 0 as successfull and FALSE when it failed. In PHP 0 equals also to FALSE, which would lead to the conclusion that the function failed.

if(somefunction() == 0) {
    // Do something
}

Assuming somefunction returns 0 when the function exited successfully, and FALSE when it failed, the snippet above would trigger when the return code is FALSE.

Using === prevents you from that, as it only will trigger if the response really is 0.

Ahatius
  • 4,777
  • 11
  • 49
  • 79