Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • equality is symmetric. hence, the two ifs are the same thing. Commented Sep 18, 2014 at 4:48
  • 1
    @devnull While technically true, there is quite a bit of variance in C/C++ standards as to which is better because of the possibility of silent typos. Commented Sep 18, 2014 at 5:13
  • In addition to the answers given, writing if ('constant' == $variable) can also eliminate bugs. If you mistakenly write (a = true) instead of (a == true) you've introduced a bug in your code. (true = a) will give a compiler error. Commented Sep 18, 2014 at 8:40
  • Just wanted to add that having the constant first in the comparison is referred to as a Yoda condition (or notation apparently): > Using if(constant == variable) instead of if(variable == constant), > like if(4 == foo). Because it's like saying "if blue is the sky" or > "if tall is the man". Coding Horror New Programming Jargon Wikipedia Yoda Conditions (Sorry I would rather have added this as a comment but I don't have the reputation) Commented Sep 18, 2014 at 8:53
  • both are wrong, the correct formulation is (a) Commented Sep 18, 2014 at 9:07