Is one more readable than the other? At first, I disliked the false ===
approach but as I see it more and more often, I'm warming up to it. I'm pretty sure they return identical results.

- 3,095
- 3
- 29
- 50
-
I guess it's like saying that if `x = y` then `y = x` – JCOC611 Nov 04 '12 at 19:34
-
5The second is easier to mis-type as an assignment without causing an error. – David Thomas Nov 04 '12 at 19:35
-
1http://wiert.me/2010/05/25/yoda-conditions-from-stackoverflow-new-programming-jargon-you-coined/ – Dogbert Nov 04 '12 at 19:36
-
2Good old [Yoda](http://www.codinghorror.com/blog/2012/07/new-programming-jargon.html) – vascowhite Nov 04 '12 at 19:43
-
I see I was wrong, they don't always return identical results, which breaks the `x = y` and `y = x` analogy. – Nick Pickering Nov 05 '12 at 15:21
3 Answers
There's absolutely no functional difference.
I usually prefer to place the variable first, and the constant value second. Because that makes sense (When you speak it out loud, would you say "I test that the variable is false"? Or "I test that false is equal to the variable"?)

- 172,118
- 50
- 264
- 308
-
1
-
1Modern IDEs will alert you if you use an assignment inside an if statement, so the only use case for putting the constant first diminishes rapidly. – Sven Nov 04 '12 at 20:10
-
I greatly prefer
false === $var
Namely because sometimes you are only using equality and not looking for identity.
In which case you write
false == $var
But sometimes you aren't at the top of your game, and might write
false = $var
which will give an immediate error, and let's you fix it right away.
However, if you type
$var = false
you end up beating your head against a wall for an hour trying to figure out why your script isn't working right.

- 5,177
- 1
- 17
- 24
-
1
-
-
I don't like `false == $var`, I prefer `! $var`. Make comparison with boolean constant only with triple equality. – fred727 Jun 10 '23 at 12:44
A much better software engineer than me taught me about this. Long story short, placing the constant first is a best practice, although it may look weird at first. I say that it's a best practice because it results in the most predictable outcomes:
$var = null;
if (false === $var) { // NullPointerException is not thrown; only evaluates to "true" if the value "false" was assigned to $var
...
}
if ($var === false { // NullPointerException will be thrown
...
}
if (false = $var) { // Parse Error
...
}
if ($var = false) { // the value false is assigned to $var
...
}

- 1,112
- 8
- 12