About the PHP Formatter

What is a PHP formatter?

A PHP formatter rewrites cramped or inconsistent PHP with proper structure: functions and classes laid out to the PSR-12 coding standard, one statement per line, and indentation that follows your blocks. This tool uses a genuine PHP parser (Prettier's PHP plugin, bundled locally and run in a background worker), so the formatting is parser-grade — strings, heredocs, match expressions, attributes, and modern PHP 8 syntax are all understood rather than guessed at.

Parsing has a second benefit: real syntax checking. Broken PHP produces a red badge quoting the parse error and its line instead of scrambled output. And because the parser runs in a web worker, even large files format without freezing the page. Nothing is fetched from a CDN and your code never leaves the browser.

How to Use This Tool

  1. Add your code. Paste PHP into the left editor (include the <?php tag), open a file, or click Sample.
  2. Watch it format. The PSR-12 result appears on the right as you type; syntax errors show up in the badge with a line number.
  3. Tune the style. Choose indentation, and pick PSR-12 new-line braces or attached 1TBS braces.
  4. Take the result. Copy it or download it as a .php file.

Common Use Cases

  • Adopting PSR-12: Bring legacy files up to the modern PHP-FIG standard in one pass.
  • Cleaning up inherited code: Make an old project readable before you start changing it.
  • Checking syntax quickly: The parse badge catches the missing semicolon before you even upload the file.
  • Reviewing snippets: Format code from tickets, diffs, and tutorials consistently.

Hashing passwords? See the PHP Password Hash Generator. Cleaning markup? The HTML Formatter. All tools are in the tools index.

Frequently Asked Questions

What coding standard does it follow?

PSR-12, the current PHP-FIG coding style, by default — opening braces for classes and functions on their own line, four-space indentation, and normalized spacing. Prefer attached braces? Switch the Braces option to 1TBS.

Does it support modern PHP syntax?

Yes. The underlying parser handles PHP 8-era syntax: match expressions, attributes, enums, named arguments, arrow functions, nullsafe operators, and constructor property promotion, alongside everything older.

Why does my snippet show a parse error?

The formatter uses a real parser, so the input must be syntactically valid PHP, starting with a <?php tag for plain code. The badge quotes the parser’s message with the line number, which usually identifies the problem instantly.

Does formatting change behavior?

No. The parser reprints your code from its syntax tree — statements, expressions, and strings are preserved exactly; only layout changes.

Why is there a brief "Formatting…" state?

The parser runs in a background web worker so big files never freeze the editor. Typical snippets format in a few milliseconds; the badge simply reflects the round trip.

Is my code uploaded anywhere?

No. Both the parser and the formatter are bundled with the page and run locally — no CDN calls, no server round trips, no logging. The tool works offline once loaded.