A php code beautifier that works


A tool to reformat your php code and make it more readable

After searching for a php code formatter, I failed finding any good tool that will not break my code.

I wanted a tool that reformats php code to match my coding guidelines, so I coded my own solution.

So, here it is. A php code reformat tool that just works, at least on the code I tested. I'm releasing the code under the GPL license and everyone is invited to submit code on which this tool fails so I can improve it and make it better.

Test the Php code beautifier

The code is implemented as a class, a php file beautify.php is provided, so you can use it as a command line tool.

php beautify.php /myproject/sourcecode/ /myproject/beautifiedcode/

Or you can implement the class and use it however you want

	include 'PhpBeautifier.inc';
	$beautify = new PhpBeautifier();
	$beautify -> tokenSpace = true;//put space between tokens
	$beautify -> blockLine = true;//put empty lines between blocks of code (if, while etc)
	$beautify -> optimize = true;//optimize strings (for now), if a double quoted string does not contain variables of special characters transform it to a single quoted string to save parsing time
	$beautify -> file( 'test.php', 'beautified.php' );

The beautifier strips all lines and leading spaces from your source code and then adds spaces and new lines only where is needed. You can modify the beautifier class code to suit your taste.

I will try to make everything optional. For now, you can specify only if you want to put space between tokens ( parenthesis ) or empty lines between code blocks ( loop structures, if structures etc).

You can even choose to optimize your code by putting double quotes to strings only where necessary. Every double quoted string that does not contain a variable or special character will be transformed to a single quoted string.

For the next version I plan to make a indenter, or maybe a reformatter for html insides php code. For now, inline html remains untouched.

I also intend to reformat multidimensional arrays so they can be easier to read. For now, they are put on single line :(.

You can download the class, the command line tool and the example from here

Share this with the world




what about the new Zend Studio for Eclipse? It has an extensive code formatter built-in that you can configure to have every detail match your coding guidelines. Then, you can export those settings and have other people in your team import it. The formatting while working on a file is then only a single keyboard shortcut away.

Sure, it costs money, but Zend Studio (for Eclipse) has been worth a lot of money for ages in my humble opinion. It supports development in PHP in so many useful ways that it's worth its price.

Posted on 2008-05-12 14:09:29

Hey there, been looking for this. The online tool seems to work quite nice, but i can't download you class. Maybe you should take a look. Thanks

Posted on 2008-05-14 07:39:57
Benjamin Kuz

This is a pretty nice piece of work - with only a few beefs.

1) You should include the option to use spaces instead of tabs - some places require that kind of thing since editors sometimes do funky things, like include half-tabs or other strange quirks which make the code come out of alignment. I hacked this in manually in order to make it work.

2) the first line of a comment sometimes \"stacks\" with the line above it, giving the effect of:

# my comment


some_code();# my comment

Posted on 2008-07-04 23:51:26

Well done! A minor glitch with heredoc, though:


shouldn't join the last lines to "XXX);" but leave them as they are.

Posted on 2008-09-18 17:50:53

Nice code, although it seems to suffer from problem that all php beautify scripts I've seen.

php has for sometime had alternative syntax for control structures. I understand that these are the preferred syntax (although old school users aren't likely to switch to them).

But it would be nice to beautify if...end, while...endwhile, for...endfor etc.

Posted on 2008-11-24 13:55:04

Doesn't actually work properly, repeated iterations continually add spaces between lines of a multiline comment, single line comments frequently pulled up onto the preceding line.

Thanks for the effort though.

Posted on 2009-11-20 21:09:51

Nice work but there are some problems with that.


will become

if( $imagePath );

I think you should fix that.

Posted on 2010-09-01 09:34:51

This is awesome - in a few weeks I will be making a site that lets people upload .php scripts to it, and I think it would be great if when they upload a script there is one uniformed formatting for them.

This is actually what I was most worried about, since I was thinking the end-user might get frustrated if the scripts don't look similar at least. Definitely going to give this script a shot. =)

Posted on 2010-12-09 14:36:47
Sean A.O. Harney

Hey, I downloaded the .zip file and found one small problem!
PHP Fatal error: Multiple access type modifiers are not allowed PhpBeautifier.inc on line 47

Line 47 of PhpBeautifier.inc has the private modifier twice, I removed one of them (doesn't matter which :) and it works! Below is Line 47 for anyone else who had the same trouble:

private private function sort($a, $b)

Although I suppose if you are indenting php code, you already know enough php to figure it out.

Posted on 2013-02-21 07:21:36

Make yourself heard



All Posts

All Comments

© Copyright CodeAssembly

All code is licensed under LGPL, unless otherwise noted