Over the past few years, I have learned many different programming languages. Having typed program after program (“Hello World” after “Hello World”), I have come up with a very specific programming style for formatting my code. Instead of just throwing a bunch of code into a text editor – hoping that I can decipher it when I look at it again in a few months – I carefully indent all of my code to the correct width, and use a lot of comments. Below, I have put together some of my most-used practices so that you can (hopefully) benefit from and build off of them.
1. Indenting Two Spaces
One of the most obvious ways to make your code more readable is to indent all of your lines to the correct width. I usually use two spaces for my tabs, an option you can specify in most text editors.
2. Code Bracket Placement
As I said before, it’s always nice to keep your code style the same throughout all of your code. One of the things I am a little “finicky” about is the placement of the code brackets that surround my code blocks (if statements, while loops, etc.).
I place my beginning bracket one space after the ending parenthesis of the code block header. Take a look at this image to see what I mean:
3. Comment, Comment, Comment
Alright, I admit it, I’m a comment freak. I go wild with comments all over the place, and I have descriptions of almost every line of code in my files. I find that if I stick to one format for all of my comments, it makes the overall readability a whole lot better.
When I comment whole sections of code, using the comment as a sort of “header”, I put my header text inside a multi-line comment, and place this right before the beginning of the section of code that I’m describing.
When describing just a single line, I use a single-line comment, and tab it one or two tabs from the line I’m describing – like this:
4. Parenthesis and Brace Padding
To make my code more readable, I add spaces inside all of my parentheses and square braces as a kind of “padding” for them. This can look strange at first, but if you get used to it, you’ll see how much more readable it can make things.
Conclusion
I hope that everyone who reads this post gets something out of it, and applies it to their own coding. Now don’t misunderstand me – this is not the only way to format your code – there are so many different ways you can do this, and the important thing is that you find the style that you’re comfortable with and stick with it.
Until next time, happy coding!




June 18, 2008 in 














Unlike me… who just writes a load of the code into a file and makes it nice and messy so only I can understand it.
Really, Really great article.
However, I think this should be filed under php also… you rarely have to do this with HTML.
Another thing is the general syntax for docblocks (as they are called) is /** (line) ** (line) **/, which is colored differently in IDEs and interpreted by robots (if you ever need that done) as standard documentation.
And http://aptana.com/ is my favorite ide, and it colors everything, and offers a “format” function, which is nice if you copy+pasted stuff, wrote quickly and forgot to indent/space etc.
And– Don’t worry about the size of your comments, they won’t increase load time.
The Zend articles are very good covering this.
Another tidbit is that it is MUCH better to use <?php to open php, and, if your file is php-only, you don’t need closing tags, in fact, that decreases the processing time.
Iain Nash – http://codejoust.com/
Great post, I really need to start doing this with my code since I am a total novice in PHP. Thanks to Iain as well.
What about structuring your code into classes and functions? Then you have come somewhere. And read Code Complete.
I am big on comments, but extra comments just waste space – I only read two lines of your first example, and I think the second comment shouldn’t be there:
Code: unset($_SESSION[“useredit”);
Comment // unset the useredit variable
A better comment would be *why* are you unsetting it every time it is set – perhaps it shouldn’t be a session variable?