PHP Beautifier

Indent a PHP file by its brace structure, formatting each PHP region separately and leaving any surrounding HTML alone.

PHP source
Formatted PHP
Runs locally in your browser

About the PHP Beautifier

A PHP file is often two languages at once. This beautifier splits the input at every <?php and ?> boundary, reformats only the code regions, and passes the HTML between them through byte for byte. A short block such as <?= $title ?> sitting inside a template line is recognised and kept inline so your markup does not gain stray line breaks.

Inside a PHP region, indentation comes from brace depth. Namespaces declared with a block, class bodies, method bodies, closures, match arms and array literals written with square brackets all add a level. case and default labels sit one step inside their switch. A closing brace that is followed by else, elseif or catch keeps that keyword on the same line.

Heredoc and nowdoc bodies are treated as single opaque units, including the closing identifier, so a SQL query or an HTML fragment stored in one is never reindented. That is important because PHP 7.3 and later use the indentation of the closing marker to strip leading whitespace from the string, and changing it would change your data. Both // and # comment styles are recognised.

Variables, arrow operators, null coalescing and typed properties are all just tokens to the scanner and come out unchanged. If you want the PHP array literal for a JSON document rather than a reformat, use JSON to PHP Array.

How to use

  1. Paste a .php file or a template into the left pane, or press Sample for a small invoice class.
  2. Leave Indent at 4 spaces to match PSR-12, the style most PHP projects follow.
  3. Turn Split statements onto new lines off when the file already has the line breaks you want.
  4. Check the inline HTML came through unchanged, then Copy or Download the file.

Common questions

Is my inline HTML reformatted too?
No. Anything outside a PHP tag is copied out exactly as it arrived. Run it through the HTML Formatter separately if you want that part tidied as well.
Does the output follow PSR-12?
It gets close on indentation, brace placement and switch labels, but PSR-12 also covers line length and import ordering, which are not applied here. Use PHP-CS-Fixer locally for full compliance.
Will a heredoc be reindented?
No. The whole heredoc, including the terminating identifier, is copied verbatim because its indentation affects the string value in modern PHP.
Are Blade or Twig templates supported?
Only the PHP regions are formatted. Blade and Twig directives sit in the HTML portion, so they are passed through untouched rather than reindented.