NameSilo

PHP: if ($day == 'Monday') or if ('Monday' == $day) Variable first or ?

SpaceshipSpaceship
Watch

deadserious

Established Member
Impact
6
I've asked this before, but would like to see what everyone thinks about it now.

Is it better to place the variable first or last? Which do you prefer?

placing it first:
if ($day == 'Monday')

placing it last:
if ('Monday' == $day)
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
I normally use if ($day == 'Monday'). The other way around just doesn't seem right to me.
 
0
•••
always variable first in comparison operations.
 
0
•••
Originally posted by Jeanco
always variable first in comparison operations.

Yeah, you want to know if it is an instance of something.
 
0
•••
We have always used: ($day == 'Monday'), as well.
 
0
•••
Honestly it doesnt matter. Either way you are going to get the true or false result. So it doesn't matter which but it has become a standard in almost all languages to do variable then comparison then what you are comparing.
 
0
•••
It's interesting. I've been around awhile and never seen a programmer put the variable second in comparisons. It would work either way, I'm just wonding where you picked up that style of coding and what the logic behind it was?

if ('Monday' == $day)
 
0
•••
Someone suggested that it was a better way to do it to for error checking reasons is where I picked it up.

So if you accidentaly make the mistake of using one = sign where you should be using two in a comparison it will give you an error and point you right to the line of code with the problem rather than replacing the value of the variable. But I still use variable first because it's just natural to me.
 
0
•••
Well the way I learned was placing variables first. It also makes it MUCH easier to an editor to read your code that way.


BTW: Deadserious please clear your PM inbox. I have been trying to PM you but it won't let me.
 
0
•••
yea fully
 
0
•••
yes. all php code i have done is variable first.. putting it opposite makes my life pretty hard. very difficult to read... bleh.
 
0
•••
CatchedCatched

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back