PHP Formatter

Formatters

How to use the PHP Formatter

Format PHP code in three steps:

1

Paste or upload PHP code

Paste PHP code (with or without <?php tags) into the input panel, or upload a .php file using the upload button.

2

Choose your indent size

Select 2 or 4 spaces. PSR-12 mandates 4 spaces; some teams prefer 2 for denser codebases. Pick what matches your project.

3

Format, copy, or download

Click 'Format PHP' to apply indentation and syntax highlighting. Copy the result or download as a .php file.


When to use this tool

Use the PHP formatter for code that needs PSR-12-consistent formatting before sharing or committing:

  • Formatting PHP copied from tutorials, Stack Overflow, or documentation that lost its indentation
  • Standardising PHP code across a Laravel, Symfony, or WordPress project before code review
  • Cleaning up auto-generated PHP from code generators, scaffolders, or AI assistants
  • Reformatting legacy PHP 5 code toward PSR-12 structure before a modernisation refactor
  • Tidying PHP snippets for technical blog posts, API documentation, or framework tutorials
  • Quickly formatting PHP interview or code challenge answers for readable submission

Frequently asked questions

Q:Does this formatter follow PSR-12?
The formatter applies PSR-12's core structural rules: 4-space indentation (configurable), consistent brace placement (K&R-style for methods — opening brace on same line as declaration), and proper block indentation for all control structures. It does not enforce PSR-12 whitespace rules around operators, blank line limits between methods, or import ordering — a full PHP_CodeSniffer or PHP-CS-Fixer tool in your pipeline handles those.
Q:Does it support PHP 8 features like match expressions, enums, and arrow functions?
Yes — match expressions, arrow functions (fn => syntax), constructor property promotion (public function __construct(public readonly string $name)), fibers, and PHP 8.1 enums (enum Status: string) are all recognised and indented correctly. PHP 8-specific keywords are highlighted in the syntax output.
Q:How are PHP variables highlighted?
All PHP variables starting with $ (including $this, $that, $$variableVariables, and ${curly} syntax) are highlighted in orange, making them visually distinct from keywords and function calls. This is particularly helpful when reviewing dense PHP code with many variable references.
Q:Can it handle mixed HTML and PHP files?
The formatter processes the entire file as PHP. In files that mix HTML and PHP (common in older WordPress templates and plain PHP views), the PHP blocks are indented correctly. Pure HTML sections between PHP closing (?>) and opening (<?php) tags are preserved as-is without modification.
Q:Does it work with Laravel, Symfony, and WordPress code?
Yes — the formatter works with any syntactically valid PHP code regardless of the framework. Laravel Eloquent models, Symfony services and controllers, WordPress plugin functions, and Drupal hook implementations are all formatted correctly. Framework-specific annotations like Laravel's Blade directives in .blade.php files are not processed — only pure PHP.
Q:What is PSR-12 and why should I follow it?
PSR-12 (PHP Standard Recommendation 12) is the coding style standard published by the PHP-FIG (Framework Interoperability Group) and adopted by Laravel, Symfony, Composer, and most major PHP projects. Following PSR-12 makes your code immediately recognisable and readable to other PHP developers, simplifies code reviews, and ensures compatibility with automated tools like PHP_CodeSniffer.