PHP Formatter

Reformats PHP with consistent indentation, brace placement and spacing. Useful for reading inherited code, tidying a snippet, or seeing what a file looks like under a standard style.

Live demo · real result from this tool
Indent

Formatted PHP

<?phpnamespace App;class Cart {    private array $items = [];    public function add(string $sku, int $qty = 1): void {        if ($qty<1) {            throw new \InvalidArgumentException('Quantity must be positive');        }        $this->items[$sku] = ($this->items[$sku] ?? 0) + $qty;    }} 

Indentation and spacing were normalised by brace structure — this is not a full PHP formatter, so line wrapping, blank-line rules and token spacing beyond the basics are left as written.

Processed on your device — nothing is uploaded.

How to use PHP Formatter

  1. Paste your PHP, including the opening tag.
  2. Read the formatted output.
  3. Copy it back, or set up PHP-CS-Fixer for anything you maintain.
  4. Formatting changes whitespace only — never behaviour.
  5. Omit the closing ?> in files containing only PHP; trailing whitespace after it breaks headers.
  6. Your code stays in the browser and is not transmitted.

PSR-12 is the standard worth following

PHP-FIG’s PSR-12 settles the questions teams otherwise argue about: four spaces rather than tabs, opening brace on the same line for control structures and on its own line for classes and methods, one space after control keywords, visibility declared on every property and method. Practically every modern PHP project follows it, which means code written to it reads as familiar to anyone joining. It superseded PSR-2, which you will still see referenced in older documentation.

The closing tag you should omit

In a file containing only PHP, leave off the closing ?&gt;. Any whitespace or newline after it is sent to the browser as output, and that is the classic cause of “headers already sent” errors — a trailing blank line in an included file, invisible in the editor, breaking every redirect and cookie on the site. Omitting the tag makes the whole class of bug impossible. PSR-12 requires it.

Use a real tool in a real project

PHP-CS-Fixer and PHP_CodeSniffer with the PSR-12 ruleset do this properly, understand PHP’s syntax, and can be run in a pre-commit hook and in CI so formatting never appears in a diff. A web formatter is for a snippet you are reading. The value of automatic formatting is not tidiness — it is that code review stops containing style arguments and starts being about the code.

Frequently asked questions

What PHP standards are used?
The formatter follows PSR-2 style guidelines including 4-space indentation, opening braces on new lines for classes, and consistent spacing.
Can it format PHP mixed with HTML?
The tool works best with pure PHP code. For mixed PHP/HTML, it will format PHP blocks but may not perfectly handle HTML context.
Does it check PHP syntax?
This is a formatter, not a validator. It improves code structure but doesn't check for syntax errors. Use PHP's built-in syntax checker for validation.
What is PSR-12?
The PHP-FIG coding style standard: four-space indentation, defined brace placement, explicit visibility, and rules for imports and line length. It is what nearly all modern PHP projects follow, and it superseded PSR-2.
Should PHP files end with a closing tag?
Not if the file is pure PHP. Any whitespace after `?>` is emitted as output and causes “headers already sent” errors that are extremely hard to trace, because the offending character is invisible. PSR-12 requires omitting it.
Does formatting affect performance?
No. Whitespace is discarded when the file is parsed, and opcode caches store the compiled result regardless. Formatting is entirely for the people reading the code.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close