Page 1 of 1

PHP formatting practices

Posted: Sat Mar 26, 2011 5:40 am
by tzirtzi
Hello again all :)

I have a hopefully very quick query about generally accepted practices when writing PHP. PHP ignores most whitespace, giving you a lot of functionally identical options when writing your code. I was wondering whether there were some clear, widely accepted practices around this, or whether it's a matter of personal preference?

I've always been in the habit of writing PHP with a minimum of whitespace. I put a line break in (1) after a semicolon; (2) after a left curly bracket; (3) before and after a right curly bracket. So like this:

Code: Select all

function x(){
	callfunction($arg1,$arg2);
	callanotherfunction($longargment1,$longargment2,$longargment3,$longargment4);
}
Other developers put in a lot more whitespace, like this:

Code: Select all

function x ()
{
	callfunction ( $arg1, $arg2 );
	callanotherfunction (
		$longargment1,
		$longargment2,
		$longargment3,
		$longargment4
	);
}
Is there a "right" way to do things here? It seems pretty much personal preference as to which is actually easier to read (I find all those extra line breaks make it very hard to read, but evidently other people don't!) but I wondered whether I should be changing to match the way other people do it.

Re: PHP formatting practices

Posted: Sat Mar 26, 2011 5:52 am
by social_experiment
I prefer using less whitespace, the second example you pasted is however what javascript functions look like when i create one. To get the indent i tab once and keep lines to a length of less than 80 characters.

The zend framework coding standard is a coding convention that is also pretty popular.

Re: PHP formatting practices

Posted: Sat Mar 26, 2011 6:38 am
by blakewilliams
I've always been a fan of less whitespace. The second example you posted looks like the PEAR coding standard.

Although I prefer the first method, I have read that the second is more useful when using editors that support brace matching. It allows for much simpler navigation of code. I've always been more of the old school, 'read it til you find it' kind, myself.

Here are a few links:

http://pear.php.net/manual/en/standards.php
http://www.dagbladet.no/development/phpcodingstandard/
http://framework.zend.com/manual/en/cod ... rview.html

Though most resources point to a line break before opening curly braces, I just can't seem to break the habit, and I think it looks better. 8)

-Blake

Re: PHP formatting practices

Posted: Sat Mar 26, 2011 12:53 pm
by Weirdan
Though most resources point to a line break before opening curly braces, I just can't seem to break the habit, and I think it looks better.
Over the years I found I can read any consistently indented and formatted code. Typing habits, on the other hand, are not easy to change. So now I type opening curly brace on the same line and it's my editor that puts it on the next line (if required), adds closing brace and puts proper amount of indent, all on the fly, without me stopping typing or press any additional buttons. Thankfully Vim is easy to configure that way.

Re: PHP formatting practices

Posted: Mon Mar 28, 2011 4:09 am
by tzirtzi
Thanks for all your replies! :)

It's good to hear that there are other people writing php in the same sort of style that I am and that there's nothing strictly wrong with it. Thanks Weirdan for your suggestion of Vim - working from that thought I've looked around and it seems that there are various bits of software around that can reformat php, so I'll go on writing the way I like safe in the knowledge that if it becomes an issue, we can automatically standardise the formatting of everything ^^