PHP Formatter Online — Format PHP Code

Beautify PHP classes, methods, traits, and arrays per PSR-12 conventions — Allman braces for declarations, K&R for control structures, 4-space indent — 100% in your browser.

What is a PHP Formatter?

A PHP formatter reformats PHP source code per PSR-12 conventions — Allman braces for class and method declarations, K&R braces for control structures, 4-space indentation, and consistent spacing around operators. It is the fastest way to clean up legacy PHP, WordPress theme code, or generated Doctrine entities.

PSR-12 is the de facto coding standard for modern PHP frameworks (Symfony, Laravel, Slim, CakePHP) and the only standard most package maintainers accept. The OpenFormatter PHP formatter applies PSR-12 defaults; PHP-CS-Fixer with the @PSR12 ruleset is the equivalent CLI tool for project-wide enforcement.

How to format PHP — 4 steps

  1. Paste your PHP. Copy a class, method, or script into the Input panel. Click Load Sample to try a demo Service class.
  2. Click Format. The formatter applies PSR-12 brace placement and 4-space indentation client-side.
  3. Review the output. The Output panel shows beautified PHP ready for PhpStorm, VS Code, or your editor of choice.
  4. Copy the result. Click Copy to paste the formatted PHP into your editor or pull request.

Side-by-side: unformatted vs PSR-12 PHP

Unformatted

<?php
class Order{public function total():float{$t=0;foreach($this->items as $i){$t+=$i->price;}return $t;}}

PSR-12 Formatted

<?php
class Order
{
    public function total(): float
    {
        $t = 0;
        foreach ($this->items as $i) {
            $t += $i->price;
        }
        return $t;
    }
}

PSR-12 Compliant

Allman braces for classes and methods, K&R for control structures, 4-space indent, no tabs — the modern PHP standard.

Behaviour Preserved

PHP is whitespace-insensitive outside strings. Formatted source compiles to identical Zend opcodes.

Client-Side Only

Code with database credentials, API keys, and proprietary business logic never leaves your browser. Verify in DevTools → Network.

Common use cases

  • check_circleFormat Laravel controllers, models, and migrations to PSR-12
  • check_circleReformat Symfony bundle code generated by Maker bundle
  • check_circleBeautify Doctrine entities generated by orm:generate-entities
  • check_circleClean up WordPress theme PHP for readability before commit
  • check_circleFormat legacy PHP 5 code as you migrate to PHP 8
  • check_circleReformat PHP copied from documentation or Stack Overflow
  • check_circleNormalise PHP after a merge conflict resolution
  • check_circleBeautify PHP generated by a code generator or scaffolding tool

Online formatter vs PHP-CS-Fixer vs PHP_CodeSniffer

An online formatter is the fastest tool for one-off snippets — paste, click, copy. PHP-CS-Fixer is the most flexible CLI: configurable rulesets (@PSR12, @Symfony, @Laravel), 200+ rules, runs in CI and as a pre-commit hook. PHP_CodeSniffer (phpcs / phpcbf) is the older alternative with broader ruleset support including WordPress, Drupal, Magento, and Yii. Use this online tool for quick paste-and-format; configure PHP-CS-Fixer in your composer.json for repository-wide enforcement.

More than formatting

Format HTML, JavaScript, or write Markdown documentation — all browser-side.

Frequently Asked Questions

What is PSR-12?

PSR-12 is the PHP Standards Recommendation for extended coding style, ratified by the PHP-FIG in 2019. It supersedes PSR-2 and adds rules for declare statements, namespace placement, return type declarations, nullable types, and short closures. The OpenFormatter PHP formatter applies PSR-12 brace placement and 4-space indentation defaults.

Which brace style does PHP use?

PSR-12 specifies a hybrid: classes, traits, interfaces, enums, and functions/methods use Allman braces (opening brace on its own line). Control structures (if, for, foreach, while, switch, try) use K&R braces (opening brace on the same line). This is unique to PHP — most other languages pick one style and stick with it. The formatter applies this hybrid by default.

Does formatting PHP change how it executes?

No. PHP is whitespace-insensitive outside string literals (single-quoted, double-quoted, and heredoc preserve their interior). The Zend engine compiles formatted and unformatted source to identical opcodes. The only exception: heredoc/nowdoc syntax requires the closing identifier to start at the beginning of the line — the formatter does not adjust those.

How are imports (use statements) ordered?

PSR-12 does not mandate a specific order, but the dominant convention (Symfony, Laravel) is: classes first, then function imports (use function), then constants (use const). Within each group, alphabetical order. PHP-CS-Fixer with the @PSR12 ruleset applies this. The online formatter preserves your existing order — it does not sort imports.

Does it format PHP 8 features?

Yes. Match expressions, named arguments (foo(x: 1)), nullable types (?string), union types (string|int), intersection types (Foo&Bar), attributes (#[Route(...)]), readonly properties, and constructor promotion are all formatted with PSR-12 conventions. The arrow function syntax (fn ($x) => $x * 2) stays on a single line.

How is this different from PHP-CS-Fixer or PHP_CodeSniffer?

PHP-CS-Fixer and PHP_CodeSniffer (phpcs/phpcbf) are full code-style fixers — they apply hundreds of rules from configurable rulesets (@PSR12, @Symfony, @Laravel), sort imports, and detect style violations. This online formatter is lighter: it normalises indentation and brace placement only, ideal for snippets pasted from documentation, Stack Overflow, or generated code. For project-wide enforcement, configure PHP-CS-Fixer in your composer.json and a pre-commit hook.

Is the PHP I paste sent to your servers?

No. Formatting runs entirely in your browser using JavaScript. Code containing database credentials, API keys, .env values, or proprietary business logic never leaves your device. Open DevTools → Network and click Format to verify.

Does it handle WordPress or Drupal coding standards?

Partially. WordPress Coding Standards use tabs, K&R-everywhere braces, and Yoda conditions — none of which match PSR-12. Drupal uses 2-space indentation. The formatter defaults to PSR-12 (4 spaces, hybrid braces). For WordPress or Drupal compliance, configure PHP_CodeSniffer with the WordPress-Core or Drupal coding standards instead.

PHP Formatter Online — Format PHP Code with PSR-12