Page 1 of 1
IF statement order
Posted: Sat Oct 06, 2007 2:39 am
by rmccue
I've noticed that in some major projects, like Wordpress, IF statements always have the string first.
Code: Select all
if('hello' == hello()) {
}
//As opposed to
if(hello() == 'hello') {
}
Now, I'm sure I read somewhere why they do this, but I don't remember why or where from. Can someone clue me in?
Thanks in advance.
Posted: Sat Oct 06, 2007 2:51 am
by stereofrog
Writing a constant first is a good habit because it protects you from the common mistype of "==" as "=". if($var = 1) will compile and the error can be tricky to find, but "if(1 = $var)" will report an error immediately.
Posted: Sat Oct 06, 2007 4:26 am
by rmccue
stereofrog wrote:Writing a constant first is a good habit because it protects you from the common mistype of "==" as "=". if($var = 1) will compile and the error can be tricky to find, but "if(1 = $var)" will report an error immediately.
Ah, thanks for that. I knew it was for a good reason

I personally trust myself enough to know not to do that
