2

I was reading a piece of code and saw that the author of the code uses the following if statement

if ( 'string' == $var )

where as I always use

if ( $var == 'string' )

What i'm wondering is if this has any other function except for style/looks. Is comparing a $var to a 'string' quicker/slower than a 'string' to a $var?

Jebble
  • 266
  • 1
  • 11
  • http://stackoverflow.com/questions/29910258/what-is-the-difference-between-a-2-and-2-a/ – Hanky Panky May 04 '15 at 11:23
  • 1
    It's called yoda condition! – Rizier123 May 04 '15 at 11:23
  • $var ='string' ; if ( 'string' == $var ){ echo "HI"; } No differenece echoes HI – aΨVaN May 04 '15 at 11:23
  • 1
    The difference is if you make a typo and use `=` instead of `==` – Mark Baker May 04 '15 at 11:24
  • Also see: http://stackoverflow.com/a/28840422/3933332 – Rizier123 May 04 '15 at 11:25
  • no difference since $var will be executed by the compiler as 'string' itself. Its just better practice and formatting of code rather than the issue of right and wrong. – Harigovind R May 04 '15 at 11:26
  • Maybe the author was a Java developer. Because String comparision isn't the same, you don't use == but a method, equals(). So if you call `var.equals("String")` and var is `null`, you'll get an exception. The solution is to write `"String".equals(var)`. – Ulti May 04 '15 at 11:38
  • I know for a fact the author is an PHP developer but your comment makes total sense =) – Jebble May 04 '15 at 11:39

1 Answers1

2

Why if( constant == variable ) is preferred instead of if ( variable == constant )

Here is an answer to your question. This helps in debugging if you miss an equal sign.

Because that form makes it harder to introduce a bug by forgetting one of the equals signs. Imagine if you did this:

if (k = 5) This was intended as a comparison, but it's now an assignment! What's worse, it is legal, and it will mess up your program in multiple ways (the value of k is changed, and the conditional always evaluates to true).

Contrast this with

if (5 = k) This is not legal (you cannot assign to a literal) so the compiler will immediately flag it as an error.

in the words of Hanky 웃 Panky in comments:

They are both equivalent but 'string'==$var is a better approach because it helps in debugging if you miss an equal sign.

Community
  • 1
  • 1
rahulroy9202
  • 2,730
  • 3
  • 32
  • 45
  • 1
    This is a comment not an answer! – Rizier123 May 04 '15 at 11:23
  • 2
    It would have been a very nice answer had you improved your statement to make it complete. Something like `They are both equivalent but 'string'==$var is a better approach because it helps in debugging if you miss an equal sign.` – Hanky Panky May 04 '15 at 11:28
  • 1
    Even though it's not an answer to my question it is in fact what I secretly was looking for without realizing it myself. I've always been using the k = 5 notation but do realize now that apparently it's bad practice or well the latter is preferred. I'll see if I can get myself to switching over to 5 = k :) – Jebble May 04 '15 at 11:34