PHP Formatter

Format PHP code with Prettier's PHP plugin: PSR-12-style braces, consistent indentation and spacing. Supports PHP up to 8.4 syntax and runs in your browser.

Output

Runs entirely in your browser. Your data is never uploaded.

Related Tools

View All

What is PHP Formatter?

The PHP Formatter reformats PHP source code using Prettier with the official @prettier/plugin-php. It produces PSR-12-like layout: braces on their own line for classes and functions, spaces around operators, and consistent indentation.

How to use PHP Formatter

  1. Paste PHP code (starting with <?php) into the input box.
  2. Choose the Indentation (4 spaces is the PSR default).
  3. Click Format (the output also updates while you type).
  4. Copy or download the formatted code.

Example

Input
<?php function f($a){return $a+1;}
Output
<?php function f($a)
{
    return $a + 1;
}

When to use PHP Formatter

Use it to clean up legacy PHP, format snippets for documentation or check that code parses. For projects, run the same formatter (or PHP-CS-Fixer / PHP_CodeSniffer) in your toolchain so everyone gets identical output. Mixed HTML/PHP templates are formatted but complex templates may look different from hand-written layout.

How PHP Formatter works

The code is parsed by php-parser inside @prettier/plugin-php and printed with Prettier's algorithm, which fits lines into 80 columns and applies the plugin's PSR-12-inspired rules. The PHP version is set to 8.4, so modern syntax (enums, readonly properties, match, attributes) is understood. Parse errors are reported with line and column.

Common Use Cases

  • Reformatting old PHP files before refactoring
  • Formatting code samples for blog posts or answers
  • Checking whether a PHP snippet has syntax errors
  • Normalising code pasted from different editors

Security Note

Everything runs locally in your browser; the code you paste is not uploaded.

FAQs

Is this exactly PSR-12?

It follows Prettier's PHP style, which is close to PSR-12 but not identical in every detail.

Which PHP versions are supported?

Syntax up to PHP 8.4 is parsed.

Does it run my code?

No, the code is only parsed and printed, never executed.

Explore more categories