How to format a PHP file

Paste a snippet or a complete file and press PHP Code Formatting. Indentation is rewritten with one tab per level inside the browser; the code is never executed, no output is produced and no part of it is uploaded.

The result keeps the passages that are literal output rather than code: the body of a heredoc or nowdoc, a # or // comment, and the HTML between a closing ?> and the next opening tag. Those regions come back byte for byte, which is what a template needs, because a stray re-indent there changes the rendered page.

  1. Paste the PHP over the sample text in the field above; a fragment, a class or a whole template file all work the same way.
  2. Press PHP Code Formatting. The result appears underneath, coloured with the PHP grammar rather than as plain text.
  3. Press Copy to put the plain code on the clipboard, colours left behind, or select single lines from the result if you only need a few.
  4. Edit the field and run the formatter again if something looks wrong: until you do, the previous result is dimmed and labelled as belonging to the earlier input.

What the formatter changes, and what it leaves alone

What the formatter changes

Every statement that was written on one line is split at its semicolon, braces move onto their own lines, and each level of nesting gets one more tab. That turns a minified class into something readable, and it also means the output no longer matches the input line by line: blank lines are removed, tabs are always used for indentation, and a long array or argument list is left on a single line instead of being rewrapped. A project that uses four spaces needs a pass with its own formatter after the paste.

What is copied unchanged

A heredoc or nowdoc body is literal output, so it is copied byte for byte, including the indented closing marker allowed since PHP 7.3. Line comments starting with # or // are treated as comments and not as braces, which matters for a block such as # see the {placeholder} syntax, and a PHP 8 attribute such as #[Route('/x')] is still treated as code because an attribute does not open a comment. Text between ?> and the next <?php belongs to HTML and is never re-indented, so a template loop such as foreach (...): with an inner <?= ... ?> comes back with its markup intact.

Where the tool stops

The tabifier reads characters, not a parse tree: it never runs the code, never checks syntax, and an unfinished brace or a missing quote comes back as written instead of as an error message. The colouring runs after the layout, and past roughly 120 KB it is skipped so that the page stays responsive, which leaves a large file readable but plain. For anything beyond a quick cleanup, php-cs-fixer, PHP_CodeSniffer or the formatter built into an editor will preserve the styles and the blank lines of a real project, and they can be run on the whole tree rather than on one paste.

Recent tools: