2

While using an 'if' statement to check whether a variable is an empty string or not we can write it in two ways i.e. if('' == $variable) and if($variable == ''). I want to know what is the impact of above in different cases?

Thanks in advance!!

  • equality is symmetric. hence, the two ifs are the same thing. – devnull Sep 18 '14 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. – Chris Pitman Sep 18 '14 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. – Dennis_E Sep 18 '14 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) – Paddy Sep 18 '14 at 8:53
  • both are wrong, the correct formulation is (a) – kevin cline Sep 18 '14 at 9:07
11

In a modern language, you should be writing your conditions of the form if($variable == ""). This makes the condition easier to read for natural English speakers.

In a legacy language, it sometimes was considered good practice to use the form if("" == $variable) as if you used the more natural form it was possible to create compilable and runnable code which was bugged if you accidentally missed one of the '=' symbols.

Now, most compilers, even for these older languages, will at least warn you if you accidentally miss the '=' symbol.

TL;DR; - use if($variable == "")

  • 4
    The line between modern/legacy languages isn't all that clear. For example in C (a legacy language?), you can have integers in if statement (anything not a 0 is "true"), so the = vs. == bug is a real concern. But even in Java, if you have booleans, both if(a=false) and if(a==false) are legal, but the result is different! (And at least my compiler doesn't warn about this case - why would it, because either case is perfectly correct code.) IMO that's a good reason to use if(""==$variable) exclusively. – Joonas Pulakka Sep 18 '14 at 6:36
  • 1
    @JoonasPulakka Decent C & C++ compilers warn about assignments in if conditions, precisely because it's a common source of error. You normally silence the warning by parenthesising the assignment - it's a way to signal to the compiler "yes, I know what I'm doing." – Reinstate Monica Sep 18 '14 at 7:00
  • @Angew Funny how you seem to suggest that the Java compiler is not 'decent' :) – marczellm Sep 18 '14 at 8:35
  • 1
    @marczellm In some way, yes. I feel that "perfectly valid, but most probably erroneous" code warrants a warning, if the compiler also provides a simple way of overriding it. – Reinstate Monica Sep 18 '14 at 8:38
6

This highly depends on the language used!

If == is implemented as a method in an object oriented language then the construct if ($variable == "") could lead to an exception because the object ($variable) might not be initialized or even null.

If you reverse the expression (if ("" == $variable)) then you can be sure that the object acted on ("" is always initialized and never null).

As an example in Java (where the method is called .equals()):

string.equals("")

can cause a NullPointerException because the object string may be null.

"".equals(string)

cannot lead to a NullPointerException as the object "" is never null.

If == is not a method of an object then it does not matter which order is used.

I think that many programmers with such a background use the expression if ("" == $variable) because they are more familiar with it.

  • I don't use Java much, and I never thought about "".equals. Thanks for the tip :). – TMH Sep 18 '14 at 9:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.