IF statement order

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rmccue
Forum Commoner
Posts: 27
Joined: Thu Oct 05, 2006 12:47 am
Location: Gold Coast, Australia

IF statement order

Post 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.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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.
rmccue
Forum Commoner
Posts: 27
Joined: Thu Oct 05, 2006 12:47 am
Location: Gold Coast, Australia

Post 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 :D
Post Reply