Formatters
PHP Formatter
PSR-flavored PHP formatting in the browser — classes, closures, arrays and match expressions done right.
In short
Format PHP online with the prettier PHP plugin: PSR-style output for classes, functions, arrays and mixed HTML/PHP. Exact error positions. Free.
By NaveenKumar T · Updated
- Industry-standard formatting
- Exact error line & column
- 2/3/4-space or tab indent
Runs entirely in your browser — nothing you paste or open here is uploaded, logged, or stored.How we handle data →
Example
PHP Formatter: input and output
A PHP 8 config loader: a backed `enum`, constructor property promotion with `readonly`, a named argument, a `match` expression and a heredoc — with the class body squashed onto a handful of lines.
<?php
declare(strict_types=1);
enum Env:string{case Prod='prod';case Dev='dev';}
final class ConfigLoader{
public function __construct(private readonly string $path,private Env $env=Env::Dev){}
public function load():array{$raw=file_get_contents($this->path);$data=json_decode($raw,true,flags:JSON_THROW_ON_ERROR);
return match($this->env){Env::Prod=>array_merge($data,['debug'=>false]),Env::Dev=>array_merge($data,['debug'=>true,'log'=>'stderr'])};}
public function banner():string{return <<<TXT
Loaded {$this->path}
env={$this->env->value}
TXT;}
}
$loader=new ConfigLoader(path:'/etc/app.json',env:Env::Prod);
echo 'keys: '.count($loader->load()).PHP_EOL;<?php
declare(strict_types=1);
enum Env: string
{
case Prod = "prod";
case Dev = "dev";
}
final class ConfigLoader
{
public function __construct(
private readonly string $path,
private Env $env = Env::Dev,
) {}
public function load(): array
{
$raw = file_get_contents($this->path);
$data = json_decode($raw, true, flags: JSON_THROW_ON_ERROR);
return match ($this->env) {
Env::Prod => array_merge($data, ["debug" => false]),
Env::Dev => array_merge($data, ["debug" => true, "log" => "stderr"]),
};
}
public function banner(): string
{
return <<<TXT
Loaded {$this->path}
env={$this->env->value}
TXT;
}
}
$loader = new ConfigLoader(path: "/etc/app.json", env: Env::Prod);
echo "keys: " . count($loader->load()) . PHP_EOL;
Single-quoted strings without `$` or escapes become double-quoted, the `match` arms line up one per line with a trailing comma, and the heredoc body is re-indented relative to the new code depth while its closing marker keeps the same offset.
Learn more
Formatting PHP, properly
This page runs the @prettier/plugin-php engine — a real PHP parser supporting modern syntax (8.x match expressions, enums, attributes, arrow functions) and producing PSR-12-aligned style: consistent brace placement, argument wrapping, array formatting. Mixed PHP-in-HTML files format too.
Legacy PHP arrives in every imaginable style; normalizing a pasted function before reading it saves real time. It's equally useful for WordPress snippets from forums — format first, audit second — with parse errors pinpointed when the snippet was truncated.
Help
Frequently asked questions
Everything you need to know about the PHP Formatter.
01How do I format PHP code here?
Paste your PHP (or load a file/URL) and press Format. The @prettier/plugin-php engine parses modern PHP — match expressions, enums, attributes — and reprints it in PSR-12-aligned style, including mixed PHP-in-HTML files.
02Which PHP versions does the formatter understand?
Modern ones — the parser tracks current PHP including 8.x features (enums, readonly properties, attributes, match). Ancient constructs that PHP itself removed may be rejected; the error position will show exactly which line relies on them.
03Does the formatter change my string quotes and heredocs?
Plain strings, yes: with `singleQuote` off, `'prod'` becomes `"prod"` whenever the literal contains no `$`, backslash or quote of either kind, so the value never changes; a string that would need escaping keeps its original quotes. Heredoc and nowdoc bodies are kept verbatim except for indentation, which shifts with the surrounding code — the closing marker moves by the same amount, so under PHP 7.3+ flexible-heredoc rules the resulting string is identical. `match` arms and wrapped array elements get one line each plus a trailing comma.