Hudzilla.org - the homepage of Paul Hudson
Contents > Writing PHP > Coding style Wish List | Report Bug | About Me ]

19.10.1     Comments and whitespace

This is NOT the latest copy of this book; click here for the latest version.

You should use spaces, tabs, and new lines wherever possible to help improve readability. Operators should have a space either side, function parameters should have a space after the comma, etc. As an example, use line two rather than line one:

$foo=func($foo,$bar,$baz);
$foo = func($foo, $bar, $baz);

Furthermore, use braces even when they are optional. Without braces readability is lost, and logic errors are possible. As an example, use option two rather than option one:

/* option 1 */
if($foo == $bar) print "Foo equals bar!";

/* option 2 */
if ($foo == $bar) {
    print
"Foo equals bar!";
}

Note that there is a space after the if statement in option 2 - this makes it clear that if is not any sort of function.

With regards to comments, avoid using # comments - /* */ and // are much more common, and do all that is required. Use comments throughout your code - it will help jog your memory when you come back to edit scripts months from now, and it will also help other programmers understand your code.

Many people split up complex lines of code into multiple lines, and this usually has a positive effect on readability. Although it has not been done in this book so that space is conserved, I usually pad out complex arrays and SQL queries, like this:

array(
    "foo" => "bar",
    "baz" => "wombat"
)

CREATE TABLE foo (
    ID INT NOT NULL PRIMARY KEY,
    Name CHAR(255)
);

I think you will agree that both of those are much more legible than trying to cram the same information all on one line. Remember: using lots of whitespace does not slow your scripts down one whit, so use whitespace liberally.





<< 19.10 Coding style   19.10.2 Variable naming >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is nine plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow