PHP Prettier: format, beautify and minify PHP in your browser
Inherited PHP is rarely tidy. A file that has passed through a template generator, three developers and a Stack Overflow answer usually carries mixed tabs and spaces, braces in two different places, ragged array alignment and lines running past 200 characters. This PHP formatter takes that source, tokenises it, and re-prints it as clean, standards-compliant code — without ever sending a byte to a server.
How the formatter decides where the lines go
The tool runs a tokenise → normalise → print pipeline. First the source is split into tokens: inline HTML, <?php and <?= tags, single- and double-quoted strings, heredoc and nowdoc bodies, comments, numbers, variables, identifiers and operators. Then the enabled normalisations run — array() to [], quote style, import sorting, declare(strict_types=1) insertion. Finally an indentation-aware printer emits the tokens, opening a level on { and on an alternative-syntax :, closing it before the matching } or endforeach;, and re-attaching each comment to the statement it annotates.
The critical property is that whitespace between tokens is the only thing that changes. String literals, heredoc bodies and comment text are opaque to the printer, so a formatted file computes exactly what the original did.
PSR-12, PSR-2, 1TBS and Allman
PSR-12 is the current PHP-FIG standard and the default here. Its most-noticed rule is asymmetric brace placement: the opening brace of a class, interface, trait, enum or named function goes on its own line, while the brace of an if, foreach or while stays on the statement line. The reasoning is that a declaration is a heading — giving its brace a line of its own makes the signature, which may span several lines of parameters, visually separate from the body. A control structure is a single short line, so the extra break would only add noise.
PSR-2 is the older standard PSR-12 superseded; it shares the brace rules but assumes an 80-character soft limit and says nothing about the closing tag. 1TBS (the style Prettier's PHP plugin prints) keeps every opening brace on the same line, and Allman gives every brace its own line. Any of them is defensible; what matters for a codebase is picking one and applying it everywhere, which is what the preset picker is for.
Beautify, minify and diff-check
Beautify is the default: re-indent, re-space and re-wrap. Minify runs the same token stream in reverse, dropping comments and emitting each token with the minimum separator needed to keep the file parseable, then reports the byte saving. Diff check formats the file and shows a line-level diff against the original — the fastest way to tell a reviewer “this commit is whitespace only”, or to confirm that a file already matches your house style before you touch it.
Templates, long lines and the limits
Mixed HTML and PHP view files are handled as a single indent stream: block-level markup opens and closes indent levels, alternative syntax such as foreach: / endforeach; does the same, and short echo tags stay inline with the text around them. A <li><?= $item->name ?></li> stays on one line; the <ul> wrapping it does not.
The tool also reports the minimum PHP version your syntax requires — match and ?-> mean 8.0, enum and readonly mean 8.1, fn() means 7.4 — alongside line counts, declaration counts, the longest line and the deepest nesting level, so an unfamiliar file can be sized up before you start editing it. When the source will not tokenise, you get the message, line and column instead of output: broken input never produces half-formatted code.