ShotMark
Skip to Content

PHP Formatter

Format PHP code with PSR-12 conventions.

PHP Input
Formatted
Waiting for input

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 phpcs and clash with hosted code review tooling.
  • Standardize control structure spacing. A space after if, for, while, and switch, and no space after function calls is a small rule that produces consistent visual cues across the codebase.

How to use the PHP Formatter

  1. Paste your PHP code into the Input panel. Include the opening <?php tag.
  2. Click Format.
  3. Review the result in the Output panel. Parse errors appear above the input.
  4. 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.

RuleConvention
Indentation4 spaces, never tabs
Line lengthSoft limit 120 characters
Class braceOn its own line (Allman)
Control structure braceOn the same line as the keyword (K&R)
Method visibilityRequired (public, protected, private)
KeywordsLowercase (true, false, null)
File endingTrailing newline, no closing ?> for pure PHP files
Operator spacingSingle space on each side
Use statementsOne 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.

Like this tool?

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.

Private beta accessFounding pricing lockNo spam ever