1

Original question

i read sometime ago in an article i can't find that when doing a comparison the order matters for some reason.
i couldn't find any information on the subject of the order of comparing in PHP.
it obviously won't make a dramatic change but i am curious to know if there is any merit to this.

throughout the project i am working on the comparison is done as $x === true.
is there any difference in doing the comparison in the opposite order, as in true === $x?

Conclusion

Apparently what i referred to in the original question is a programming style called "Yoda conditions".
This wiki page gives a good explanation about this style.
This answer made me understand the concept, just note there is a small mistake there.

Here's my take on it:

The main reason to use this style is to avoid an accidental assignment with = when you meant to compare with ==.

  • if you want to check if a variable loosely has the same value as what you compare it to, use:

    if(false == $var) // evaluates to true if $var is equal to false
    

    over

    if($var == false) // evaluates to true if false is equal to $var
    

    to prevent

    if($var = false) // assigns false to $var and evaluates to false
    

    while

    if(false = $var) // is a syntax error
    
  • if you want to check if a variable strictly has the same value as what you compare it to, use:

    if(false === $var) // evaluates to true if $var is identical to false
    

    over

    if($var === false) // evaluates to true if false is identical to $var
    

    it doesn't matter in the context of using Yoda style or not as this isn't really an issue with strict comparison because it's pretty hard to confuse = with === but if you use it for == then also use it for === to be consistent.

All in all, I think the merit of Yoda notation lies in the emphasized distinction it makes between comparison and assignment in a condition.

Rtzoor
  • 308
  • 2
  • 11
  • 3
    it's a preference – treyBake Jan 27 '20 at 16:14
  • 3
    It's generally referred to as "yoda notation" or "yoda comparison" – Brett Gregson Jan 27 '20 at 16:15
  • 2
    Does this answer your question? [Does variables' order matter when doing == and === comparisons?](https://stackoverflow.com/questions/19352107/does-variables-order-matter-when-doing-and-comparisons) Note that this question is also about the order of comparisons, not `==` vs `===` as many people mistook it to be. – Run_Script Jan 27 '20 at 16:18

1 Answers1

4

Those are called "Yoda Comparisons". For === and == it would not matter, but consider this:

if($a = 0)

In this case $a is being set to zero, not compared. In addition, the comparison you think you're making will not evaluate at all, but the comparison operation will proceed as if there was a comparison made. It will always fall through to false Flip it:

if(0 = $a)

Now an error would be thrown.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • 1
    This is because `=` is an assignment operator. Only `==` and `===` are comparison operators. – Run_Script Jan 27 '20 at 16:22
  • 1
    I could not understand what you meant until i read this https://stackoverflow.com/a/19352170/10390841 , and then i read your answer again and i understood it. consider adding output to make it clearer. – Rtzoor Jan 28 '20 at 10:04
  • 1
    "the blue is sky" :) – John Smith Nov 22 '22 at 11:10