In this example we are going to examine some tools that will beautify or properly format PHP source code.
It can become neccessary to do this, if source code has been minified or wrongly formatted.
Below we present some other reasons you might need a source code beautifier:
- You’ve downloaded an unreadable script: You are about to develop a particular app but you don’t know how to go about it, so you download an open source code for an app which is similar. The only problem is that the editing style for the code makes it difficult or rather impossible to read. Beautifier tools come in handy for a situation like this.
- Years ago you made a script that doesn’t fit in your current style: Before you became the pro that you are now, you wrote a script that is quite still relevant in the present day and you want to look it up again, the only problem is that the code styling is bad. It is really not a problem if you have access to any of the beautifier tool mentioned below.
- You want to publish your script, but it has to fit in the PEAR style.
- You might be working on a project with a couple of developers whose editing styles are quite diffrent and you want a single readable style applied to the code.
- You want to read an already minified source code.
- You were contacted to continue working on a script written by a developer whose editing style is bad and makes it quite difficult to read and understand the code or even improving it.
1. Getting Started
Quite often when writing PHP code, indentation, spacing, and other formatting can come out wrong. It is also common for multiple developers to work on a single project who has different formatting techniques. The tools mentioned below can become handy for making a perfectly formatted source code.
It is also common for PHP scripts to be minified or obfuscated. You can also use these tools to make PHP source code look pretty and readable so it is easier to edit. Most or all of the tools mentioned below are online tools( they link to different websites).
Any good beautifier tool should not modify / change the code. It should just give the code proper indentation and newlines.
Note: The list below is not in any specific order and it’s also not exhausitive.
List Of Tools
- PHP Beautify
- PHP Beautifier
- PHP Formatter
1.1 PHP Beautify
According to the website hosting PHP beautify, all you need to do is enter your messy, minified, or obfuscated PHP code into the code editor to have it cleaned up and made pretty. The editor above also contains helpful line numbers and syntax highlighting.
There are quite a number of options to tailor beautifier to your personal formatting tastes. You can visit PHP Beautify at PHP Beautify.
1.2 PHP Beautifier
According to the website you can use PHP Beautifier to Beautify dirty, ugly PHP code using Online PHP Beautifier and make your PHP code more readable. It gives the PHP code proper indentation with spaces or tabs. It also supports various indentation styles such as K&R style, Allman style, Whitesmiths style and GNU style.
PHP Beautifier is quite easy to use, it simply involves copying and pasting your code in the provided edit or text box and clicking on the format button. The edited PHP source code appears in the text box. Below the text box it’s containing your unbeautified source code.
You can visit PHP Beautify at PHP Beautifier.
1.3 PHP Formatter
PHP formatter is another PHP source code beautifer tool that looks promising though it is still in beta. It has features that not only rival the tools mentioned above, it also seems to beat them.
PHP Formatter looks like a beautifier tool that will hold its own. According to the web app website the core of PHP Formatter was written in C++, because of the speed. The PHP source code is first scanned and parsed according to PHP grammar files. The node tree of the PHP code is then put through the formatter, which decides where to put spaces and indentations.
This way, PHP Formatter can’t misinterpret anything. On top of that, parsing the files gives the opportunity to show syntax errors as PHP would.
Features of PHP formatter are:
- Formatted code works exactly the same as non-formatted code.
- It supports PHP 4 and PHP 5 (up to version 5.2.8).
- There is a built in syntax check, which highlights the line of the error.
- You can format a lot of files at once, and download them as ZIP.
- It handles alternative style structures, which is handy for templates.
- It handles DOC style comments.
- It aligns assignment statements.
- Option to link PHP functions to the manual. (Handy for functions you don’t know!)
- It lets you define your own style
As per the web app website the best feature of the tool is the fact that you can make your own styles. You can also choose from some standard styles. (eg: PEAR standard).
There are quite a few options to define a style. You can also Make an unlimited number of styles.
Some of the style formatting options are:
- You can set where you want spaces and where not.
- You can set the number of spaces (or tabs) to indent.
- Option to color the code, delete newlines and/or comments and comment after control structures.
- There’s also a handy manager for adding, deleting or changing styles.
PHP formatter seems like a real good beautifier tool with a lot of promise. You can visit the web app at PHP Formatter
Another source code beautifier tool worthy of mention is PHP_beautifier. It is open source and was created by Claudio Bustos (Lead Developer) and jesús Espino (Developer). This program reformat and beautify PHP source code files automatically. It is written in PHP 5 and reformates PHP 4 and 5 source code.
You can download PHP_beautifier at PHP_beautifier.
In this example we learnt about PHP beautifier tools, What they are, why we need them and how they make PHP development a lot easier. One or more of the tools above should be in every PHP developer toolkit.