0

What is the difference between the 2 statements:

if (false === $variable) {
  //do something
}

and

if ($variable === false) {
  //do something
}

I personally use the second style but often run into code in frameworks that i use which always seem to use the first style.

Is there a difference ( i suspect it is some legacy thing to do with types) or is this simply a coding habit (it must be rooted in something though??)

If not, what is the reasoning behind the first style given that the logic is actually backwards.

Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
  • possible duplicate of [Why do some experienced programmers write expressions this way?](http://stackoverflow.com/questions/3309089/why-do-some-experienced-programmers-write-expressions-this-way) – mario Mar 24 '12 at 10:47
  • possible duplicate of [php false place in condition](http://stackoverflow.com/questions/9438043/php-false-place-in-condition) – mario Mar 24 '12 at 10:48

6 Answers6

0

If you put the value that can't have something assigned to it (a literal, a constant, a function call, etc) on the left hand side then, if you accidentally use an assignment operator (=) instead of a comparison operator, then you'll get an error rather then a hard-to-track-down bug.

i.e.

if (false = $variable) { // Error
if ($variable = false) { // Valid code that doesn't do what you want
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

This dates to C programming. It's a way to make sure "compilers" will fail if you accidently used = (assignment) and not == or === (equals) because "false" is not an lvalue (can't be on the left side of an assignment)

Chen Harel
  • 9,684
  • 5
  • 44
  • 58
0

It stems from this:

if (false = $variable)

syntax error

if ($variable = false)

accidental assignment

Corbin
  • 33,060
  • 6
  • 68
  • 78
0

if ($foo = false) (note: = instead of ==) is a typical typo that leads to hard to debug problems. For that reason, some people prefer false == $foo, since the runtime will throw an error when you try to assign something to false.

Other than that there's no difference.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

It has been around for a while. It comes from accidentally writing (by forgetting one =):

if (c = 1) {
}

which does assignment and it's doesn't actually check whether c equals 1. It assigns 1 and then it evaluates to 1 (so the if statement becomes always true due to that mistake). By writing

if (1 == c) {
}

as a habit, you cannot make that mistake. If you would forget the = in this case, the compiler would warn you because assignment to the literal 1 is not possible.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
0

Practically, the two statements are equivalent.

So, why to write it this way or the other (?) :

  • Try to avoid a typical typo : if ($condition = false) instead of if ($condition == false). In that case, your 2nd version would cause a difficult-to-debug issue, while the first would throw an error
  • Want to force one of the two parts of the conditional statement be evaluated first (the rightmost)
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223