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: Happy 103993/33102 Day!
natelyman: @chitownforte22 it smells like a turd covered in burnt hair!
natelyman: @mikvogel @rgiertz @chriswoods85 Vogel hit the nail on the head.
natelyman: My flight is booked for PHP Tek-X in Chicago May 18-22 Woohoo!