PHP Formatter
Format PHP code online with Prettier for PHP (@prettier/plugin-php): PSR-12 braces, spacing and 4-space indentation. No install, and nothing is uploaded.
Runs in your browser · Nothing uploadedFree · No sign-up
Something not working, or missing a feature? Send feedback
About the PHP Formatter
The PHP Formatter is Prettier for PHP in your browser: it runs the official Prettier PHP plugin (@prettier/plugin-php) to produce consistent, PSR-style code. Braces, spacing and line breaks are normalised so diffs only show real changes.
Everything runs in your browser, so your input is never uploaded to a server.
How to use the PHP Formatter
- Paste PHP code (with or without the opening <?php tag) or open a file.
- Choose indentation.
- Copy or download the formatted code.
Example
Input
<?php
namespace App;
class UserService{public function __construct(private Repo $repo){}
public function find(int $id):?User{ if($id<=0){return null;} return $this->repo->find($id);}}Result
<?php
namespace App;
class UserService
{
public function __construct(private Repo $repo) {}
public function find(int $id): ?User
{
if ($id <= 0) {
return null;
}
return $this->repo->find($id);
}
}
Class and method braces move to their own line and control-structure braces stay on the same line, as PSR-12 requires. Indentation is four spaces.
Common mistakes
- Missing <?php tag. Without the opening <?php tag the input is treated as plain HTML and returned unchanged. Add the tag, even for a single function.
- Unclosed braces. The formatter needs code that parses. An unclosed { stops formatting with the line of the syntax error; fix that line first.
- Quote style changes. Simple single-quoted strings such as 'plain' become "plain". Strings that contain $, backslashes or quotes keep their single quotes, so behaviour never changes.
- Two-space indentation. PSR-12 requires four spaces. Keep the default unless your project uses its own style.
Works well with the PHP Formatter
- PHP Compiler: run the formatted snippet and check its output.
- JSON to PHP Array: turn a JSON payload into a PHP array literal.
- Text & Code Diff: review exactly what formatting changed.
Frequently asked questions
Is the output PSR-12 compliant?
It follows PSR-style conventions for layout. Rules that need semantic changes (such as naming) are not applied.
Does it run my PHP code?
No. The code is only parsed and formatted in your browser; use the PHP Compiler to run it.
Is my PHP code uploaded?
No. Formatting runs in your browser with the Prettier PHP plugin; the code is never sent to a server.
Does it format HTML mixed with PHP?
PHP blocks are formatted. Inline HTML between ?> and <?php is kept as it is.
Is this the same as Prettier for PHP?
Yes. It runs Prettier with the @prettier/plugin-php plugin, so the output matches npx prettier with that plugin and the same options. Use it to try Prettier PHP on a file before adding it to your project.
How do I set up Prettier for PHP in my project?
Run npm install --save-dev prettier @prettier/plugin-php, add "plugins": ["@prettier/plugin-php"] to .prettierrc, then run npx prettier --write on your .php files. Add npx prettier --check to CI so unformatted code cannot be merged.
Does Prettier for PHP follow PSR-12?
Mostly. It uses PSR-12 style braces, 4-space indentation and spacing, but it also reflows long lines and normalises quotes, which PSR-12 does not require. Use PHP_CodeSniffer if you need strict PSR-12 checking.
How do I format PHP in VS Code?
Install the Prettier extension and @prettier/plugin-php in your project, or paste the file here for a one-off format. The result is the same because both run Prettier with the same plugin.
Can I format a WordPress or Laravel file?
Yes. Paste the whole file including the opening <?php tag. Classes, closures, attributes and match expressions are supported; Blade templates (.blade.php) are not PHP and need their own formatter.
Guides and tutorials
- Programming Languages
How to Format PHP Code Online: Beautify, Indent and Follow PSR-12
Format PHP code online: paste messy PHP, get consistent indentation and spacing, learn what PSR-12 requires and when to use Prettier, PHP-CS-Fixer or phpcbf.
· 4 min read
- Code Quality & Workflow
Prettier for PHP: Format PHP Code with @prettier/plugin-php
How to use Prettier for PHP: install @prettier/plugin-php, set up .prettierrc, choose braceStyle and phpVersion, format on save and check PHP formatting in CI.
· 7 min read
Related tools
JSON Formatter
Format and beautify JSON with configurable indentation and optional key sorting. Errors are reported with exact line and column.
HTML Formatter
Format and beautify messy or minified HTML into clean, indented markup with Prettier, including embedded CSS and JavaScript. Runs locally in your browser.
CSS Formatter
Format and beautify CSS, SCSS and Less online with Prettier. Turn minified or messy stylesheets into readable, consistently indented code in your browser.
JavaScript Formatter
Format and beautify JavaScript and JSX online with Prettier. Choose indentation, quotes, semicolons and line width; your code is formatted in your browser.
TypeScript Formatter
Format and beautify TypeScript and TSX online with Prettier, including types, generics and decorators. Choose indentation, quotes and semicolons.
SQL Formatter
Format and beautify SQL queries online for MySQL, PostgreSQL, SQLite, SQL Server and more, with keyword casing and indentation options. Runs in your browser.