PHP Formatter

Format PHP with Prettier.

Runs locallyYour input is processed in this tab. The editor blocks outgoing network requests.
PHP input

The PHP Formatter reformats PHP with Prettier for PHP, producing layout close to PSR-12: braces on their own line for classes and methods, four-space indentation, and wrapped arrays and method chains. It is for Laravel, WordPress and plain PHP developers tidying a class, config file or snippet. Everything runs in your browser — nothing is uploaded.

How to format PHP code

  1. Paste your code, open a .php file with ⌘/Ctrl O, or load an example. The input must start with <?php (or contain PHP tags) to be treated as PHP.
  2. Decide on Single quotes: leave it off to normalise plain strings to double quotes, or turn it on to match codebases that prefer single quotes.
  3. Copy the result with ⌥/Alt C, download it with ⌘/Ctrl S, or choose Switch to HTML for template markup.

Examples

Class with constructor promotion

PHP 8 constructor property promotion, typed parameters and return types are kept, and each method gets PSR-12-style braces.

PHP
<?php
namespace App;class Greeter{public function __construct(private string $name){}
public function greet(bool $excited=false):string{return "Hello, ".$this->name.($excited?"!":".");}}

Result:

PHP
<?php
namespace App;
class Greeter
{
    public function __construct(private string $name) {}
    public function greet(bool $excited = false): string
    {
        return "Hello, " . $this->name . ($excited ? "!" : ".");
    }
}

Array config file

A nested array that exceeds the print width is broken one key per line with a trailing comma. With Single quotes off, simple single-quoted strings become double-quoted.

PHP
<?php return ['db'=>['host'=>'localhost','port'=>3306],'cache'=>['driver'=>'redis','ttl'=>3600]];

Result:

PHP
<?php return [
    "db" => ["host" => "localhost", "port" => 3306],
    "cache" => ["driver" => "redis", "ttl" => 3600],
];

Laravel route group with single quotes

With Single quotes on and Print width at 100, the fluent chain breaks per call and the closure body stays on one line.

PHP
<?php
Route::middleware(['auth','verified'])->prefix('orders')->group(function(){Route::get('/',[OrderController::class,'index'])->name('orders.index');});

Result:

PHP
<?php
Route::middleware(['auth', 'verified'])
    ->prefix('orders')
    ->group(function () {
        Route::get('/', [OrderController::class, 'index'])->name('orders.index');
    });

Options explained

  • Print width — the line length Prettier aims for before wrapping arrays, argument lists and method chains (80, 100 or 120).
  • Tab width — spaces per indentation level; 4 is the PSR-12 requirement and the default, 2 is available for projects that use it.
  • Single quotes — when on, strings are printed with single quotes where possible; when off, simple strings are normalised to double quotes, while strings containing $ or backslashes keep the quotes that preserve their meaning.

Common PHP parse errors

PHP errors read Parse Error : syntax error, … followed by what the parser found and expected, with the line number.

unexpected '$b' (T_VARIABLE), expecting ';'

The statement on the previous line has no semicolon, so the parser runs into the next variable. The line reported is the one after the missing semicolon.

expecting '}'

A class, function or control block is not closed. The error is reported at the end of the input, so look upwards for the brace that was never matched.

unexpected '}'

There is one closing brace too many. The column points at the extra brace.

expecting '"'

A double-quoted string is never closed, so the parser reads to the end of the file looking for the quote. Find the string that opens on or before the last line you expected to be valid.

unexpected ')' in code that looks like JavaScript

Syntax such as const x = () => 1; is JavaScript, not PHP; PHP arrow functions need fn() => 1. Paste JavaScript into the JavaScript Formatter instead.

Nothing changed

If the input does not contain <?php, the whole text is treated as inline HTML and returned as it was. Add the opening tag at the top.

FAQ

Is the output PSR-12 compliant?

It is close. Prettier for PHP follows PSR-12 for braces, indentation, spacing around operators, declare and use statements, and control structures. It preserves blank lines rather than inserting them, so a file without a blank line after namespace or between methods keeps that spacing. Run PHP_CodeSniffer or PHP-CS-Fixer with the PSR-12 ruleset if your pipeline enforces the standard strictly.

Which PHP versions are supported?

The parser is configured for PHP 8.3, so enums, match expressions, readonly properties, constructor promotion, named arguments, attributes, first-class callable syntax and union or intersection types all format correctly. Older PHP 5 and 7 code parses too, as long as it does not rely on syntax that newer versions removed.

Can I format PHP mixed with HTML, like a WordPress or Blade-free template?

Yes, for files that use <?php … ?> and <?= … ?> blocks. The PHP inside the tags is formatted and the HTML between them is kept as written. For heavy markup, format the HTML separately with the HTML Formatter. Blade and Twig directives are template languages of their own and are not parsed here.

Is my code sent to a server?

No. Prettier and the PHP plugin run in your browser inside an isolated frame that blocks network requests, so nothing is uploaded or stored on a server. Print width, tab width and quote preference are remembered on this device; the code itself is kept only if you turn on "Remember my last input".

Why did my single-quoted strings become double-quoted?

With Single quotes off, Prettier for PHP prints simple strings with double quotes, the same default it uses for JavaScript. The change is safe: strings containing $ or escape sequences keep their original quotes so no interpolation is introduced. Turn Single quotes on to get 'localhost' instead of "localhost".

Does it fix code style issues beyond formatting?

No. It changes whitespace, line breaks and quote style only. It does not rename variables, reorder or remove unused use statements, add strict types declarations or rewrite deprecated functions. Those are refactoring or linting tasks that need a tool aware of your project's rules.

When to use the PHP Formatter

Use it to clean up a class copied from documentation, a config array exported by a script, or a controller before a code review. The formatter works on one file at a time, so it suits quick cleanup rather than reformatting a whole repository. For the JavaScript, CSS and markup that sit next to PHP in most projects, see the other code tools.

Browse all Code Formatters