PHP Formatter
Paste PHP and get clean, consistently formatted code: Prettier's PHP plugin rewrites spacing, line breaks and indentation in a PSR-12 compatible style. Prefer to keep your line breaks? Switch to Re-indent only. Everything runs in your browser.
Last updated:
Tip: press Ctrl + Enter to format; Tab indents inside the editor (press Esc first to move focus). Your code is formatted in your browser and never uploaded or executed.
How to use the PHP Formatter
- Paste your PHP into the input, open a .php file, or load a sample.
- Choose the mode: Full format rewrites the code with Prettier PHP; Re-indent only fixes indentation and blank lines but keeps your line breaks.
- Set the style: indentation, brace style, print width, quotes, trailing commas and target PHP version.
- Press Format PHP (or Ctrl + Enter). If the code has a syntax error, the line and column are shown and highlighted.
- Copy or download the formatted code, or press Use output as input to keep working on it.
PSR-12 layout rules applied by the formatter
| Rule | Example |
|---|---|
| 4 spaces per indentation level, no tabs | return $total; |
| Class, interface, trait and method opening brace on its own line | final class Cart{ |
| Control-structure brace on the same line, one space after the keyword | if ($a === $b) { |
| One space around binary operators and after commas | $x = $a + $b * 2; |
| Return types: no space before the colon, one space after it | public function total(): int |
| One statement per line; one blank line after the namespace and use blocks | namespace App;use App\Model\User; |
| Arguments split one per line when the call is longer than the print width | $this->mailer->send( $to, … |
Brace styles compared
PSR-12 / PER places the opening brace of classes, functions and methods on a new line, but keeps braces of if, foreach, while and try on the same line. 1TBS (the "one true brace style") puts every opening brace on the same line, which some teams prefer for consistency with JavaScript code. Both are applied consistently across the file, so pick the one your project's coding standard (or PHP_CodeSniffer / PHP-CS-Fixer config) uses.
Common PHP syntax errors
- syntax error, unexpected end of file: a
{is never closed; the error is reported at the end, so look upward for the missing}. - unexpected identifier / variable: usually a missing semicolon at the end of the previous line.
- unexpected string content: a quote is missing, or a variable is used inside single quotes where it is not interpolated.
- Heredoc not closed: before PHP 7.3 the closing identifier had to start at column 0; later versions allow indentation but it must match the body.
Formatting templates with lots of markup? Tidy the HTML parts with the HTML Formatter, and the queries with the SQL Formatter.
Frequently asked questions
Is my PHP code uploaded to a server?
No. The formatter is JavaScript (Prettier and its PHP plugin, which includes a PHP parser written in JavaScript) running in your browser. Your code is never sent anywhere and never executed.
Does the output follow PSR-12?
The default settings produce code that follows PSR-12 and its successor, the PER Coding Style, for layout: 4-space indentation, opening braces of classes and methods on their own line, control-structure braces on the same line, and one statement per line. Naming rules and other non-layout parts of PSR-12 are not changed by a formatter.
What is the difference between Full format and Re-indent only?
Full format parses the file and prints it again from scratch, so spacing, line breaks, quotes and long lines are all normalized. Re-indent only keeps every line as you wrote it and fixes the indentation according to brackets, switch/case blocks and continuation lines, which is useful for legacy templates or when you want minimal diffs.
Why does it say there is a syntax error?
Full format needs code that PHP can parse: missing semicolons, unclosed brackets or strings, and mismatched quotes are reported with the line and column. Re-indent only does not need valid code, but it warns about brackets or strings that are never closed.
Can it format PHP mixed with HTML?
Yes. Inline HTML outside the <?php ?> tags is kept as is, while the PHP blocks are formatted. For heavy templates, Re-indent only usually gives the most natural result.
Which PHP versions are supported?
Modern syntax up to PHP 8.3 is understood, including enums, readonly properties, match expressions, named arguments, attributes, arrow functions, nullsafe ?-> calls and first-class callable syntax. Pick a PHP version to stop the formatter from using syntax (such as trailing commas in parameter lists) that an older version would reject.
Are all the tools on FreeToolCabin tested for quality and accuracy?
Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.