Home PHP Keep your PHP clean and consistent
Keep your PHP clean and consistent PDF Print E-mail
User Rating: / 0
PoorBest 
Written by Nate Lyman   
Tuesday, 26 January 2010 02:58

Keeping your PHP clean and consistent will make your co-workers lives a lot easier, as well as your own.  I prefer development teams that have a defined coding standard.  Naming conventions, tab spaces, etc...  It is a great idea to implement coding standards in your team now, whether small or large.

It is very annoying when one opens a file and sees a combination of this:

Show/Hidden php code

I'm not going to say any style is better, but I prefer the first syntax.  Just don't mix and match, I will guarantee the person who works on your code next will standardize it.

Also use consistent naming conventions, stick with camelCase OR under_scores for variables, functions, and classes.

My next bit of advice is use switches.  Its a great way to reduce lines of code, and make it more readable.

Show/Hidden php code

As you can see, the switch statement is a lot more clear and concise.

Another thing I like to use is ternary operators.  They are an easy way to consolidate lines of code into one line.  Keeping your code easy to read.  However, you can go too far with ternary operators.  Don't nest them, unless you are screwing with someone. (I've done that before)

Show/Hidden php code

As you can see the second example has a much smaller footprint, and is a lot easier to read.

 

Last Updated on Tuesday, 26 January 2010 03:39
 

Add comment


Security code
Refresh

@natelyman

natelyman: @pioneerelite awesome. teach her how to rob old hags at the poker table
natelyman: #Bears RT @michaelombardi Darren Sharper just had his knee scoped and is on crutches so he had to cancel two visits.
natelyman: AMEN! RT @sam_trapkin: I'm just gonna put it out there that i think mayonnaise is digusting. You slimy freaks
natelyman: @joelfeder Thanks Buddy.
natelyman: Won second place out of 81 teams at an innovation competition at work. Hopefully I'll have a link to share soon when it goes live.
natelyman: Still waiting for Kanye to highjack an acceptance speech #AcademyAwards #Oscars