My Twitter

5 Tips for Coding Cleaner PHP

April 6th, 2009

PHP is lovely, though if you ever work with other people having good code is important. Here are some tips to improve your code.

Comment & Document

It’s really important that people understand why you are doing certain things in certain ways. Adding a quick comment above sections of code should be adequate, but documenting classes and functions (Even if it’s in a Wiki) is fantastic.

Give Variables, Functions and Classes Meaningful names

Nothing is worse than trying to figure out what a function called “SIDFE()” does. Give everything a name that if someone else looked at it, they could figure out what it does. The above is a real example I have come across while adjusting a clients website, if the other programmer had called it something like Scan_Incomming_Data_For_Evil() it would have been a lot more straightforward.

White space

whitespaceAs you can see, a little space here and there makes life a lot easier.

No one likes to have to search for the start of a function. Make sure you indent your code and keep it easy to read quickly.

Never Delete – Comment out

This one is a little hard to grasp, but imagine you just fixed a bug (say 100 lines of code to fix it) and something else has broken. It makes sense to be able to go back and see the old code without modification of the new code. Also, doing this helps people see where an old bug was (assuming you comment that the section of code is evil) for future reference.

Use Braces

Braces are those neat } and { things. If you don’t use them on various functions it’s a pain to figure out where a loop starts and finishes. This is especially important when programming on a large scale because no one likes debugging fugly code. Here is an example of good and bad code:

<?php
/* Examples of annoying code */
if ( $coder === 'Silly' ) bang_head(); 

while ( $coder === 'Silly' )
    bang_head();

/* Examples of good code*/
if ( $coder !== 'Silly' ){ Drink_Beer(); }

while ( $coder !== 'Silly' ){
    Drink_Beer();
}
?>

Useful Links

PHP coding guidelines
10 Useful PHP Tips Revisited

DeliciousFacebookDigg
TwitterStumbleUponGoogleLinkedInRSS Feed

8 Responses to “ 5 Tips for Coding Cleaner PHP ”

  1. Tinsley said:

    Good points. Hopefully someone will read this and stop posting untabbed code with 500 character long lines on forums :)

    Regarding the braces section, I’d argue that the second example of “annoying code” is actually good code and the first example in “good code” is actually “annoying code” but that might just be a difference in taste.

  2. Mike said:

    Point taken :) Thank you for the comment.

  3. Stevie said:

    I’d have to disagree on the ‘Never Delete’ point, that is actually really annoying. Good code should not have dead/redundant getting in the way.

    To combat the ‘need’ for not deleting, you should use some form of source control (eg, SVN – available on Sourceforge I believe) to store revisions of your code. That way your current code will be clean and you will still be able to retrieve and view an older version of the code if you require to re-implement a feature.
    Additionally, you get the benefit of being able to ‘version’ your code so you can easily retrieve an entire copy of your code base as it was at that point in time.

  4. [...] 5 Tips for Coding Cleaner PHP [...]

  5. Scott said:

    Sorry but half of this is bad advice…

    Whitespace: you example doesn’t go far enough. how about some extra line breaks, and spaces around braces and ‘while’, etc. I refuse to believe anyone writes code like you “Bad” example.

    Never Delete: no, no, no. Never leave commented code in your files, it just clutters up otherwise readable functions. It’s fine to comment stuff while testing, it obviously saves time when switching between different logic, but once everything’s working the code should be neat!

    Use braces: well this is a controversial one. Firstly your modified ‘if’ statement is as fugly, if not moreso. If there is only one statement for the ‘if’ or ‘while’ there’s no reason to use braces, just put the statement n the next line. It’s blindingly obvious that if you add another statement to the ‘if’ you’ll need to use braces.

  6. [...] 5 Tips for Coding Cleaner PHP [...]

  7. [...] 5 Tips for Coding Cleaner PHP [...]

  8. [...] 5 Tips for Coding Cleaner PHP [...]

Leave a Reply