PHP Beautifier
Enhance readability of your PHP code with optimal spacing and structure
Tidy the indentation of messy PHP
Minified snippets, code pasted from an email, or files edited by five different people tend to end up with random indentation, which makes bugs hard to see. This beautifier reads your PHP line by line and rebuilds the indentation from the curly braces, so blocks line up the way a reader expects.
Beautifying a snippet
- Paste your PHP into the Source PHP box. Load Sample inserts a deliberately untidy example and Clear empties the field.
- Under Beautify Options, decide whether to keep Space around operators ticked. A second checkbox, Follow PSR-12 indentation, is shown, but indentation is always four spaces.
- Click Beautify PHP.
- Read the result in the Beautiful Code panel and press Copy.
What it actually does
Each line is trimmed and then indented according to how many braces are open. A line that begins with a closing brace moves one level out, and a line that ends with an opening brace pushes the following lines one level in. With operator spacing switched on, the characters =, +, -, * and / are surrounded by single spaces, so $a=1+$b turns into $a = 1 + $b.
Important limitations
- Lines are never split. If several statements or braces share one line, they stay together, so heavily compacted code will only be partly improved.
- The operator step is a plain text replacement. It can also affect arrow operators such as => and ->, increment signs, comments and text inside strings. If you see odd spacing there, untick the option and run it again.
- Braces inside strings or comments are counted too, which can throw off indentation.
Situations where it helps
- Reading a function you copied from a forum answer.
- Cleaning up a small legacy script before a code review.
- Making a code sample presentable in documentation.
- Spotting a missing closing brace, because the indentation drifts.
Try the sample first to see the effect on a tangled example.
The processing is done by JavaScript in your browser and the output appears instantly. This is a lightweight text formatter, not a full PHP parser. For a whole project, use a dedicated tool such as PHP CS Fixer, and always run your tests after reformatting.
Related Tools