PHP Formatter
Standardize and clean your PHP code blocks
Standardise the layout of a PHP script
Code that has passed through several editors usually carries a mix of tabs, spaces and stray indentation. That makes diffs noisy and reviews slower. This formatter normalises the indentation of a PHP script in one click, so nested blocks line up consistently and you can drop the result straight into a repository or a code sample.
Formatting your code
- Paste the script into the PHP Script box. Use Load Sample for a compact one-line function, or Clear to start over.
- Open the Indentation dropdown and pick 4 Spaces (PSR-12), the default, or 2 Spaces.
- Click Format PHP Code.
- Review the Formatted Output panel. Below it a small bar reports the size in KB and the number of lines.
- Press Copy to copy the result, or Download to save it as script.php.
How the formatting works
Every line is trimmed of leading and trailing whitespace and re-indented from scratch. A line that starts with a closing brace steps one level back, and a line that ends with an opening brace raises the level for the lines after it. Because indentation is derived from braces alone, the result is consistent and predictable across the whole file.
Where it fits
- Converting a file indented with tabs into clean spaces before committing.
- Matching a team convention of two spaces when a snippet arrives with four.
- Preparing PHP examples for a blog post or documentation page.
- Making a pasted function readable enough to debug.
What it does not do
This is an indentation tool, not a full code style fixer. It does not split lines, so the sample function, which is written on a single line, keeps its statements together on that line. It does not reorder imports, change quote styles, align array arrows or enforce line length. Curly braces that appear inside strings or comments are treated like any other brace and can shift the indentation that follows, and multi-line array or method-chain continuations are not given extra hanging indents.
Formatting is done in your browser with JavaScript, and the download is created locally from the text shown in the output panel. If you need complete PSR-12 compliance across a project, run a dedicated tool such as PHP CS Fixer or PHP_CodeSniffer, and use this page for quick tidy-ups of snippets.
Related Tools