7

In PHP, what is an underlying reason of placing either boolean or null before identical comparison operator?

false === $value;   
null === $value;

It seems to me that it is same as saying

$value === false;

Is it just a personal preference or there is a concrete reason why people do this?

Ken
  • 83
  • 1
  • 4
  • possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – rekire Oct 17 '12 at 17:13
  • 1
    He isn't asking what some operator does, he's asking why one would do Yoda comparisons. – Gromer Oct 17 '12 at 20:03

3 Answers3

12

It's a convention to avoid the mistake of accidentally assigning a variable.

$value = false;

instead of

$value === false;
nalply
  • 20,652
  • 12
  • 75
  • 93
  • 1
    +1 , It's important to know the reason! If by mistake you write `false = $value` you'll get an error or warning , unlike `$value = false` which will make you accidentally assigning a variable.(as @nalply wrote) – Ofir Baruch Oct 17 '12 at 17:16
  • @OfirBaruch: I get this argument, I really do... but I still find it less then convincing. The majority of people write their conditions the other way around. When adopting this style, you might find yourself chasing bugs caused by `if ($false === $value)` which is an easy one to overlook. Also, when working in a team, this is the kind of code that many will change, to fit their habits... making `git blame` a bit pointless sometimes. +1 for this answer, though: it's a good point – Elias Van Ootegem Oct 17 '12 at 17:23
  • It's just a convention. I rarely use it. It feels awkward to me. For me it makes more sense in special cases like in `if (false === ($file = fopen("example.txt", "r"))) die("FAIL");` or such. – nalply Oct 17 '12 at 17:28
  • There's no right or wrong , it's just about what's comfortable for you and your code. – Ofir Baruch Oct 17 '12 at 17:28
4

This is sometimes referred to as Yoda-conditions, there's a fun list of all such constructs and their unofficial names.

No there's no real difference between $var === false or false === $var, some people claim it's easier to see what is being checked for if the bool is the left operand, other hate it... In short: personal preference is what it is.

Elias Van Ootegem
  • 67,812
  • 9
  • 101
  • 138
  • Thanks for the link. It is useful to know the terms when searching answers online. – Ken Oct 17 '12 at 17:32
-1

It's supposed to be quicker, but I can't lay a hand on an authority saying this with a simple Google search. See:

http://forums.phpfreaks.com/topic/222939-is-there-a-difference-between-ifvar-false-and-iffalse-var/

for one opinion.

Kevin_Kinsey
  • 2,023
  • 1
  • 18
  • 23
  • I'm not trying to be trollish, but I don't see much point in writing a summary of someone's opinion. I tested his opinion, and he's right, but only at scale; say, 1-1.5 seconds in 100M operations. See http://pastebin.com/XsQ8ypPS – Kevin_Kinsey Oct 17 '12 at 17:58
  • Oh, and let's mark an answer down, then ask for clarification, and then erase our question. Not nice. – Kevin_Kinsey Oct 17 '12 at 20:01
  • That's me. I realized that it is impossible to write a summary of this forum. That's why I removed the request to write the summary. Then I donwvoted your answer because I think it is not helpful for StackOverflow to link to a forum. Please read the FAQ why it is not helpful. – nalply Oct 19 '12 at 10:02