1. Home
  2. PHP Formatter

PHP Formatter

Paste messy PHP and get clean, consistently formatted code in the PSR-12 / PER style. It runs in your browser, and your code is never uploaded.

messy.php → clean.php
<?php
namespace App\Http;
use Illuminate\Http\Request;
class OrderController extends Controller{
public function store(Request $request){
$data=$request->validate(['email'=>'required|email','items'=>'array']);
if(count($data['items'])===0){return response()->json(["error"=>"No items"],422);}
foreach($data['items'] as $item){ $total = ($total ?? 0)+$item['price']*$item['qty']; }
return response()->json(['total'=>$total,'currency'=>"USD"]);
}
}
FormattedPress Format or ⌘/Ctrl + Enter
The formatted code appears here.

Powered by Prettier and @prettier/plugin-php, loaded on first use (about 250 KB).

What the PHP formatter fixes

Formatting is about making code predictable to read. The formatter rewrites the layout of your code while leaving its meaning untouched:

  • Indentation: four spaces per level, never tabs, as PSR-12 and PER require.
  • Braces: classes, interfaces and methods get their opening brace on the next line; if, foreach, while and closures keep it on the same line.
  • Spacing: one space around binary operators and after control keywords and commas, none inside parentheses.
  • Long lines: calls, arrays and conditions longer than the line width are broken over several lines, one argument or element per line, with a trailing comma.
  • Quotes (optional): double-quoted strings without variables or escape sequences become single-quoted.

PSR-12, PER and other PHP coding styles

The PHP Framework Interop Group published PSR-2 in 2012 and replaced it with PSR-12 in 2019. Since 2022 the style is maintained as the PER Coding Style, which extends PSR-12 to newer syntax such as enums, attributes, match, readonly properties and property hooks. Laravel, Symfony, Drupal and WordPress each add their own rules on top; WordPress is the most different, with tabs and spaces inside parentheses. If your project follows one of these, use its own tool so the result matches exactly.

Set up formatting in your project

An online formatter is handy for a snippet. In a project, make formatting automatic so nobody has to think about it:

# PHP-CS-Fixer
composer require --dev friendsofphp/php-cs-fixer
vendor/bin/php-cs-fixer fix src --rules=@PER-CS

# Laravel Pint (zero config)
composer require --dev laravel/pint
vendor/bin/pint

# PHP_CodeSniffer
composer require --dev squizlabs/php_codesniffer
vendor/bin/phpcbf --standard=PSR12 src

Run the formatter in a pre-commit hook or in CI with a --dry-run or --test flag, so a pull request fails when code is not formatted. In VS Code and PhpStorm you can also format on save with the same configuration.

Formatter, linter or static analysis?

A formatter only changes layout. A linter checks syntax: the PHP Syntax Checker finds parse errors for any version from 7.4 to 8.5. Static analysers such as PHPStan and Psalm go further and find type errors, calls to undefined methods and dead code. To see whether code actually runs on a given PHP version, use the PHP Version Checker or run it in the PHP Sandbox.

More PHP tools

Frequently asked questions

Which coding style does the formatter use?

It follows the PER Coding Style, the successor of PSR-12 published by the PHP-FIG: four-space indentation, opening braces of classes and methods on their own line, braces of control structures on the same line, one space after control keywords and a consistent spacing around operators. Choose 1TBS to keep every opening brace on the same line.

Will formatting change what my code does?

No. The formatter parses the code into a syntax tree and prints that tree again, so only whitespace, line breaks, quotes (if you enable single quotes) and trailing commas change. If the code has a syntax error, nothing is formatted and you get the error instead.

Is my code uploaded?

No. The formatter is Prettier with the official PHP plugin, loaded into your browser. Your code is processed locally and never sent to our server.

Can it format PHP mixed with HTML?

Partly. The PHP blocks are formatted and the HTML between them is kept as it is. For templates with a lot of HTML, a template engine such as Blade or Twig with its own formatter is the better fit.

How do I format PHP automatically in my project?

Install a formatter in the project so every developer and the CI use the same rules. PHP-CS-Fixer and PHP_CodeSniffer (phpcbf) are the most common, Laravel ships Pint, and Prettier with @prettier/plugin-php works well if your team already uses Prettier for JavaScript.