Developer Tools

PHP Formatter

Paste messy PHP and get it back cleanly laid out. Indentation, braces, spacing around operators and the wrapping of long lines are made consistent, following the PER and PSR-12 coding style that most PHP projects use. Your code never leaves the browser.

  • Runs in your browser
  • No sign-up
  • Free to use

How to use PHP Formatter

  1. Paste PHP code or open a .php file; snippets without an opening tag work too.
  2. Choose the brace style, indentation and line width.
  3. Select Format PHP.
  4. Copy the formatted code or download it.

PHP Formatter features

PSR-12 / PER style

Braces for classes and methods on their own line, control structures on the same line, four-space indentation.

Modern syntax

Understands typed properties, arrow functions, match, enums, attributes, named arguments and readonly classes.

Mixed HTML and PHP

Templates with HTML around PHP blocks are handled.

Line wrapping

Long argument lists and arrays are broken over several lines at the width you choose, with trailing commas.

Syntax check included

If the code cannot be parsed, the error and its line are shown instead of a guess.

Runs locally

Formatting happens in the browser; nothing is uploaded.

When to use PHP Formatter

  • Cleaning up code copied from a forum, an old project or a one-line minified file.
  • Bringing a contribution in line with a project's style before a pull request.
  • Making nested arrays and long conditions readable.
  • Checking a snippet for syntax errors without a local PHP installation.

PHP Formatter FAQ

Which coding standard does the formatter follow?

The PER Coding Style, the successor of PSR-12, as implemented by Prettier's PHP plugin. In short: four spaces, opening braces of classes and methods on a new line, opening braces of control structures on the same line, one space around operators and after commas.

Does formatting change what the code does?

No. Only whitespace, line breaks, optional trailing commas and, if you choose, the quote style of simple strings are changed. Strings with variables or escape sequences keep their quotes, since converting them could alter their meaning.

Which PHP versions are supported?

Syntax up to PHP 8.2 is parsed. Formatting older code is no problem, as long as it is valid PHP.

Why does the formatter report a syntax error?

It has to parse the code to format it. If a brace, a semicolon or a quote is missing, parsing fails and the position is reported. Fix the error and format again.

Can it format a snippet without <?php?

Yes. If no opening tag is found, one is added internally for parsing and removed from the result.

How does this compare with PHP-CS-Fixer or PHP_CodeSniffer?

Those are command-line tools for whole projects with configurable rule sets and automatic fixing in a build pipeline. This formatter is for quick, one-off formatting in the browser. For a team project, use one of those tools in your editor and continuous integration.

Why consistent PHP formatting matters

PHP gives authors a great deal of freedom in how code is laid out, and over the years every project developed its own habits. The PHP Framework Interop Group ended most of the argument with PSR-2, later PSR-12 and now the PER Coding Style: a shared set of rules for indentation, braces, spacing and declarations. Major frameworks and libraries follow it, which means that code formatted this way looks familiar to nearly every PHP developer.

The benefit is practical. In a consistently formatted file, structure is visible at a glance. Indentation shows nesting, each statement has its own line, and a missing brace stands out. Code review becomes about what the code does, since nobody needs to comment on spacing. Version control diffs show real changes, because no one reformats lines by hand in passing.

An automatic formatter achieves this without effort or discussion. It parses the code into its syntactic structure and prints that structure again from scratch according to the rules. The original layout has no influence on the output, so two people formatting the same code always get the same result. Lines that are too long are broken at the best syntactic point, typically one argument or array element per line with a trailing comma, which keeps later additions down to one changed line.

Formatting is no substitute for the other qualities of good code, naming, small functions, types and tests, but it is the cheapest one to get right. Running a formatter costs a second and removes an entire category of distraction.

Other useful tools