PHP Formatter
Format PHP code with PSR-12 conventions.
What is PHP Formatter?
A PHP formatter is a tool that rewrites PHP source code with the spacing, brace placement, and indentation rules defined in PSR-12 so files in a Laravel, Symfony, or WordPress codebase stay consistent. The script’s behavior never changes.
Backend developers reach for a PHP formatter when a snippet mixes brace styles, when a legacy file uses tab indentation, or when a controller needs to match the PSR-12 style enforced by php-cs-fixer in the rest of the project.
Why use a PHP formatter?
- Match PSR-12 without a local toolchain. PSR-12 is the de facto style standard for modern PHP. Formatting in the browser saves a Composer install when you only need to clean up a single file.
- Normalize brace placement. PHP allows both K&R (opening brace on the same line as the keyword) and Allman (brace on the next line). PSR-12 picks K&R for control structures and Allman for classes and functions. The formatter applies the right one in each context.
- Convert tab indentation to four spaces. PSR-12 mandates four-space indentation. Tab characters from older editors fail PSR-12 checks under
phpcsand clash with hosted code review tooling. - Standardize control structure spacing. A space after
if,for,while, andswitch, and no space after function calls is a small rule that produces consistent visual cues across the codebase.
How to use the PHP Formatter
- Paste your PHP code into the Input panel. Include the opening
<?phptag. - Click Format.
- Review the result in the Output panel. Parse errors appear above the input.
- Click Copy to copy the formatted code.
PSR-12 style conventions
The PHP formatter applies the rules in PSR-12, which extends the earlier PSR-1 and PSR-2 standards.
| Rule | Convention |
|---|---|
| Indentation | 4 spaces, never tabs |
| Line length | Soft limit 120 characters |
| Class brace | On its own line (Allman) |
| Control structure brace | On the same line as the keyword (K&R) |
| Method visibility | Required (public, protected, private) |
| Keywords | Lowercase (true, false, null) |
| File ending | Trailing newline, no closing ?> for pure PHP files |
| Operator spacing | Single space on each side |
| Use statements | One per line, grouped at file top |
Examples
A controller method with mixed brace styles and tab indentation:
<?php
class UserController{
public function show($id)
{
$user=User::find($id);
if($user==null){return null;}
return response()->json($user);
}
}After formatting, brace placement and spacing follow PSR-12:
<?php
class UserController
{
public function show($id)
{
$user = User::find($id);
if ($user == null) {
return null;
}
return response()->json($user);
}
}Common use cases
- Laravel developers reformatting controllers, models, and Eloquent scopes pulled from older tutorials before merging into a PSR-12-strict project.
- Symfony engineers cleaning up service definitions and form types where contributors used inconsistent brace styles.
- WordPress plugin authors moving from WordPress core style toward PSR-12 for new code in a hybrid codebase.
- PHP instructors normalizing student submissions for grading and comparison.
Frequently asked questions
Does this PHP formatter run my code?
No. The formatter parses and rewrites the source. It never executes the PHP, opens a network connection, or accesses any database referenced in the code.
Is this the same as php-cs-fixer?
The output targets the same PSR-12 rules but the implementations differ. php-cs-fixer is a Composer-installed tool with hundreds of configurable rules. The browser formatter applies the core conventions in one pass. For project-wide enforcement, install php-cs-fixer or phpcs --standard=PSR12.
What is the difference between PSR-2 and PSR-12?
PSR-12 supersedes PSR-2 and adds rules for modern PHP features such as strict types, nullable type hints, anonymous classes, and grouped use declarations. PSR-12 is the current standard.
Does it support PHP 8 syntax?
Yes. Constructor property promotion, named arguments, match expressions, enums, and readonly properties all parse and format correctly. Attributes (#[...]) are preserved.
Will it strip my closing ?> tag?
PSR-12 recommends omitting the closing ?> in files containing only PHP, to prevent accidental whitespace from breaking headers. The formatter removes a trailing ?> from pure PHP files. Files that mix PHP and HTML keep the closing tag where needed.
Does it format inline HTML?
No. PHP blocks are formatted. HTML between ?> and <?php is left untouched. For HTML cleanup, run the output through a separate HTML formatter.
Related tools
- YAML Formatter - clean up YAML config files used in Laravel and Symfony deployments.
- SQL Formatter & Beautifier - pretty-print SQL queries pulled from PHP database logs.
- Markdown Formatter - normalize Markdown docs in your PHP project README.
- HTML Prettify - format the HTML output produced by your PHP templates.
Related tools
ShotMark captures what you do here, in one click.
The traces, payloads, and tests you run by hand? ShotMark grabs the whole bug and hands it to your AI agent.