Skip to main content

PHP Code Formatter

Format and beautify PHP source code - re-indent by brace depth, normalize operator spacing, and choose PSR-12 mixed, K&R, or Allman brace style. Handles variables, string interpolation, heredoc/nowdoc, and -> / :: operators.

Input

Drop a file or browse
One file · text
Or

Formatting Options

Output

Formatted Code
 
Was this helpful?

Guides

Paste messy PHP and get clean, consistently indented source back instantly. The PHP Code Formatter re-indents your code by brace depth, tidies spacing around operators and punctuation, and applies the brace style you choose — all in your browser, with nothing uploaded to a server.

It is built for the moments a full IDE is overkill: a snippet pasted from a chat, a Stack Overflow answer with collapsed whitespace, generated code with no indentation, or a quick tidy-up before dropping a class or function into a code review.

How to use it

  1. Paste your PHP source into the input box, or upload a .php file.
  2. Pick your Indent (2, 4, or 8 spaces, or tabs) and Brace Style.
  3. Toggle Normalize blank lines to taste.
  4. Copy the formatted result, or download it as formatted.php.

Formatting runs automatically as you type or change an option, so you see the result immediately.

Formatting options

Indent controls the unit used for each nesting level. PSR-12, the PHP-FIG community coding standard, specifies 4 spaces — the default here — but 2 spaces and tabs are available for projects with different conventions.

Brace Style switches between three conventions:

  • PSR-12 mixed — the standard most PHP projects follow: class, interface, trait, and function declarations put the opening brace on its own line, while control structures (if, for, foreach, while, switch, try/catch/finally) keep the brace on the same line, e.g. if ($x) { but a function's { sits below its signature.
  • Same line (K&R) — every opening brace, including class and function declarations, stays on the same line as the statement above it.
  • Next line (Allman) — every opening brace, including control structures, moves to its own line beneath the statement.

Normalize blank lines collapses long runs of consecutive blank lines so the output stays compact and readable.

Does it understand PHP-specific syntax?

Yes. A PHP-aware tokenizer scans the source before anything is reformatted, recognizing <?php / <?= / ?> tags, $variables, single- and double-quoted strings (including {$expr} interpolation), heredoc and nowdoc blocks, and the -> and :: operators. Braces, commas and operators that appear inside a string, comment, or heredoc/nowdoc body are never touched — a heredoc's contents are copied through completely untouched, exactly as written, since re-indenting or re-spacing text inside one would corrupt it. Both array syntaxes, [...] and the legacy array(...), are handled identically.

Is it the same as running a real formatter?

It is a fast, dependency-free beautifier — it re-indents and spaces your code, but it does not wrap long lines, reflow argument lists, sort use imports, or enforce the complete PSR-12 specification the way php-cs-fixer or PHP_CodeSniffer would. For a quick, reliable cleanup of indentation, spacing and brace placement it is ideal; for strict CI-enforced formatting, keep using your project's formatter.

Is my code private?

Completely. All formatting happens locally in your browser using JavaScript. Your source code is never sent to a server, logged, or stored.

Which PHP versions are supported?

The tokenizer recognizes modern PHP syntax including typed properties, arrow functions (fn() => ...), match expressions, enums, readonly properties, and constructor promotion, so code from PHP 7 through the latest releases formats correctly.

phpcode formatterbeautifyindentbrace stylepsr-12

Love the tools? Lose the ads.

One payment clears every ad from your account, for good. No subscription, no tracking.