PHP Code Beautifier
Use this PHP code beautifier to turn a cramped snippet into something you can read. It puts each statement on its own line, moves braces to separate lines and indents nested blocks with 2 or 4 spaces. It is a simple text reflow, so it suits short, plain code rather than whole projects.
How to beautify PHP code
-
1
Paste your code
Put the snippet in the PHP code box. The opening tag is optional.
-
2
Pick the indent
Choose 2 or 4 spaces. Four is the common choice for PHP.
-
3
Continue to the result
Use the main button to open the formatted output.
-
4
Read it before you copy
Scan the result against your original, then use the copy button.
What beautifying means here
A beautifier changes layout only. The code should do the same thing before and after. This tool applies a short list of layout rules:
- Every semicolon ends a line.
- Every opening brace goes on its own line, and every closing brace too.
- Code inside braces is indented one level per nesting depth.
- Commas are followed by a single space.
- Blank lines in the result are removed.
It does not add spaces around operators, rename anything or reorder code.
A before and after example
Input:
<?php class User{private $name;public function getName(){return $this->name;}}
Result with 4 spaces:
<?php class User
{
private $name;
public function getName()
{
return $this->name;
}
}
Notice that the opening tag stays on the same line as the first statement when you paste everything on one line. Put the tag on its own line in your input if you want it separate.
When to use something else
The tool counts braces and semicolons as plain characters. It does not parse PHP, so it cannot follow a project style guide, wrap long lines or fix spacing around operators such as = and +. Comments, heredocs and for (;;) loops can come out wrong, because a semicolon inside them is treated like any other. For a full codebase, run a configured fixer in your editor or build pipeline and keep this page for quick one-off cleanups.
Beautifying a function with a loop
Compact input:
function total($items){$sum=0;foreach($items as $item){$sum+=$item["price"];}return $sum;}
Each brace opens a new level, so the loop body sits two levels in and return drops back to one. Reading top to bottom you can now see that the loop adds to $sum and the function returns it afterwards. In the one-line version that was hard to see.
Choosing 2 or 4 spaces
Four spaces is the common width in PHP code and is easiest to scan. Choose 2 when the code is deeply nested or the place you will paste it is narrow, such as a chat message. The setting changes only the indentation, never the line breaks.
Check the result
Beautified code is easier to review, which is the point. Read it once for missing or extra braces, because the tool counts them without reporting problems. If the indentation drifts oddly near the end, an unmatched brace in your input is the usual cause.
Frequently Asked Questions
It should not, because it only moves whitespace. Still, comments and heredocs are not recognised, so compare the result with your original and run the code.
The tool does not add spacing around operators. It handles line breaks at braces and semicolons, indentation and the space after commas.
It is not designed for that. Braces and semicolons in the HTML parts are treated like PHP, so use it for pure PHP snippets.
No. You can paste a fragment with or without the PHP opening tag.
Related Tools
Color Wheel Explorer
Explore the colour wheel interactively. Rotate harmonies, compare HSL vs RYB, spot complementary and triadic pairs in real time.
FPS Counter
Measure browser FPS with requestAnimationFrame, smoothing, min/max frame rate, warnings and an optional graph. Runs locally with no upload or API.
Placeholder Image Generator
Create a 1–4000 px placeholder with custom colors and text, download it as PNG, or copy a URL and HTML image tag.
Random Phone Number Generator
Generate 1 to 50 fictional phone numbers from officially reserved ranges for the US, Canada, UK, Australia or Germany.
GitHub Profile README Generator
Build the README.md for your GitHub profile page (the username/username repo) with tagline, skills, stats cards and language breakdown.
Color Palette Generator
Generate monochromatic, analogous, complementary, triadic or tetradic color palettes from a base HEX color and export copy-ready CSS variables.
Tool available in other languages
- مُجمّل كود PHP [AR]
- PHP コードの整形ツール [JA]
- PHP-Code-Beautifier [DE]
- PHP-kodformaterare [SV]
- Embelezador de código PHP [PT]
- PHP 코드 뷰티파이어 [KO]
- Embellisseur de code PHP [FR]
- PHP-codeverfraaier [NL]
- เครื่องมือจัดรูปแบบโค้ด PHP [TH]
- Công cụ làm đẹp mã PHP [VI]
- Embellecedor de código PHP [ES]
- Upiększacz kodu PHP [PL]
- Pemercantik Kode PHP [ID]
- Formattatore di codice PHP [IT]
- Форматировщик кода PHP [RU]
- PHP Kod Güzelleştirici [TR]
- PHP 代码美化工具 [ZH]