PHP Beautifier
Format PHP code in PER / PSR-12 style.
PHP Beautifier – Format PHP Code Online
The PHP beautifier formats PHP files with the official Prettier PHP plugin. It understands modern PHP 8 syntax and produces output that follows the PER Coding Style (the successor of PSR-12) by default, the style used by Laravel, Symfony, and most Composer packages.
Supported PHP syntax
- Namespaces,
useimports, classes, interfaces, traits, and enums. - Typed properties, constructor property promotion, union and intersection types,
readonly. - Arrow functions (
fn($x) => $x * 2), closures withuse, named arguments, and the nullsafe operator?->. matchexpressions, attributes (#[Route('/posts')]), heredoc and nowdoc strings.- Mixed PHP and HTML templates with
<?php ... ?>blocks.
Options
- Indentation – 4 spaces (PSR standard), 2 spaces, or tabs.
- Line width – 80, 100, or 120 characters; long method chains and argument lists are wrapped beyond it.
- Single quotes – prefer
'single'quotes for strings without interpolation. - Braces – PER / PSR-12 puts the opening brace of classes and methods on a new line; Same line (1TBS) keeps it on the declaration line.
How it works
The plugin parses the PHP code into a syntax tree and prints it again, so formatting is reliable even for complex expressions. A syntax error — a missing semicolon, an unclosed string, or a stray brace — is reported with the line and column.
Why use a formatter?
Consistent formatting removes style debates from code review and makes legacy code easier to read. It is also helpful for code pasted from forums or emails where indentation got lost.
Related tools
Format the HTML parts of your templates with the HTML Beautifier, SQL queries with the SQL Beautifier, and JSON API responses with the JSON Beautifier.
Frequently Asked Questions
Which PHP versions are supported?
The parser supports PHP 5.x through PHP 8.x syntax, including enums, readonly properties, attributes, match expressions, and first-class callable syntax.
Does it follow PSR-12?
With the default PER / PSR-12 brace style and 4-space indentation, the output follows the PER Coding Style, which extends PSR-12. A few details, such as line length, depend on your chosen options.
Is the code executed?
No. The code is only parsed and reprinted in your browser. It is never run or uploaded.