W3docs

Tools

PHP Code Formatter

Paste your PHP code and click Format to clean it up with Prettier. Tab size and (for JS/TS) trailing semicolons are configurable. Switch languages from the toolbar.

About the PHP formatter

This tool reformats PHP source code — indentation, brace placement, operator spacing, quote style — into one consistent style. Paste in PHP from a legacy file, a Stack Overflow answer, or a teammate's inconsistently-styled commit, and it reprints the whole snippet the same way every time. There's no project config file to write and no CLI to install; it formats exactly what you paste and replaces it with the result in the same editor. Everything runs in your browser: the code you paste is never sent to a server, which matters if the snippet contains real credentials, API keys, or business logic you'd rather not transmit.

Formatting runs on Prettier 3's standalone build plus @prettier/plugin-php — the same plugin you'd add to a local Prettier install, since Prettier doesn't support PHP natively. Prettier parses the code into a syntax tree and reprints it from scratch instead of patching whitespace with regular expressions, which is why it also rewrites array('a' => 1) into ['a' => 1] and normalizes single-quoted strings to double-quoted ones. Brace placement follows PSR-12: function and class declarations get their own line, while if/else/foreach/while keep the brace on the same line. Because the source has to fully parse, invalid PHP throws a line-and-column error instead of coming back unchanged.

Reach for this when you need to clean up a pasted snippet before dropping it into a bug report, a code review comment, or a documentation page, or when you want a fast sanity check that a block of PHP actually parses. The Tab size control (1–8 spaces) is the only formatting knob exposed — unlike this site's JavaScript/TypeScript formatters, there's no semicolon toggle, since PHP statements require semicolons syntactically and Prettier has nothing to switch. For project-wide formatting under your own rules, you'd still want prettier and @prettier/plugin-php, or php-cs-fixer, installed locally — this page is built for one-off snippets, not a CI pipeline.

Examples

Long-form arrays become short-form; quotes normalize to doublephp
// input
<?php
$arr = array('a'=>1,'b'=>2,'c'=>3);
$arr2=['x'=>1,'y'=>2];

// output
<?php
$arr = ["a" => 1, "b" => 2, "c" => 3];
$arr2 = ["x" => 1, "y" => 2];

Both the long-form array() call and the existing short-form literal come out as short-form arrays with consistent spacing around => and double-quoted keys — a syntax-tree rewrite, not a whitespace pass.

Braces relocate per PSR-12; operators get spacedphp
// input
<?php
function add($a,$b){
return $a+$b;
}

// output
<?php
function add($a, $b)
{
  return $a + $b;
}

Default tab size is 2 spaces. Function and class declarations get the opening brace on its own line; if/else/foreach keep it on the same line as the condition.

Invalid PHP throws a line/column error, not silent outputphp
<?php
function broken( {
    echo "test";
}

Formatting this throws Parse Error : syntax error, unexpected '{', expecting T_VARIABLE on line 2 — shown inline as an amber alert — instead of silently returning the input unchanged.

Frequently asked questions

Does formatting change what my PHP code does?

No. It only rewrites whitespace, indentation, brace placement, and a handful of semantically-equivalent rewrites — like turning array('a' => 1) into ['a' => 1] or normalizing quote style — that don't change behavior. The input has to be valid, parseable PHP; if it isn't, formatting throws a parse error instead of guessing at what you meant.

Why did clicking Format leave my code unchanged?

The most common cause is a missing <?php opening tag. Prettier's PHP parser treats anything outside <?php ... ?> as literal output rather than code, so a snippet pasted without the opening tag passes through untouched. Add <?php to the top of the snippet and format again.

Does it convert single-quoted strings to double-quoted strings?

Yes, and it isn't configurable here. @prettier/plugin-php normalizes string literals to double quotes regardless of whether they contain variable interpolation, which differs from the common PHP convention of using single quotes for strings that don't need it.

Is my code sent to a server when I format it?

No. Formatting runs entirely in your browser on Prettier's standalone build — the page loads the Prettier and PHP-plugin JavaScript bundles on first use, then formats whatever you paste locally. The PHP source itself is never transmitted anywhere.

Does it support modern PHP syntax like enums and match expressions?

Yes. Enums, attributes (#[...]), readonly and promoted constructor properties, the nullsafe operator (?->), named arguments, and match expressions all parse and format correctly.