Back to Tools

PHP Formatter

Standardize and clean your PHP code blocks

PHP Tool
PHP Script
Formatted Output

                

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

  1. Paste the script into the PHP Script box. Use Load Sample for a compact one-line function, or Clear to start over.
  2. Open the Indentation dropdown and pick 4 Spaces (PSR-12), the default, or 2 Spaces.
  3. Click Format PHP Code.
  4. Review the Formatted Output panel. Below it a small bar reports the size in KB and the number of lines.
  5. 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


Frequently Asked Questions

What indentation choices does the formatter offer?
You can pick 4 spaces, labelled PSR-12, or 2 spaces from the Indentation dropdown. Tabs are converted to the chosen number of spaces because each line is trimmed and re-indented from its brace depth.
Does it break long lines apart?
No. It only changes the leading whitespace of each existing line. If your function is written on a single line, as in the sample, the statements stay on that line. Add line breaks manually before formatting for a fuller effect.
Can I save the formatted code as a file?
Yes. Use the Download button to save the output as script.php, or Copy to place it on your clipboard. The file is generated in your browser from the text shown in the Formatted Output panel.
What do the size and line counts show?
The bar under the output reports the formatted text size in kilobytes and the number of lines. It helps you see how much whitespace changed compared with your input, but it is not a quality score.
Is it the same as PHP CS Fixer?
No. PHP CS Fixer understands PHP syntax and applies many style rules. This page re-indents by counting braces, so it is best for snippets and quick tidying rather than enforcing a full coding standard across a codebase.
Support Chat
We typically reply within minutes

Hello! 👋 Before we start, please tell us your name.