More games at WuGames.ioSponsoredDiscover free browser games — play instantly, no download, no sign-up.Play

PHP Formatter & Minifier

Free online PHP formatter, beautifier, minifier and PSR-12 / PER-CS compliance checker. Runs 100% in your browser, so private code never leaves your machine.

PHP Formatter - Format and Beautify PHP Online

A powerful online PHP formatter and beautifier tool that helps you format, beautify, and minify PHP code. Features syntax highlighting, PSR-2 style support, and customizable indentation. Perfect for PHP developers and backend programmers.

What does the Check PSR-12 button report, and how do I read it?

Check PSR-12 runs a fast, in-browser audit of structural style rules and lists each issue with its line number: tabs used for indentation (PSR-12 mandates 4 spaces), a closing ?> tag in a pure-PHP file, short open tags <? instead of <?php / <?=, trailing whitespace, a missing final newline, and a missing blank line after the namespace declaration. A green badge means your snippet passes these checks; a red badge counts the violations. This is a quick quality gate that mirrors phpcs/php-cs-fixer in CI — not a full AST analysis — so use it to catch the most common merge-blocking issues before you commit. Click Format afterward to auto-fix most of them (indentation, braces, and trailing whitespace) in one step.

PSR-2 vs PER-CS brace style: leave the PSR-2 checkbox enabled for the classic PSR-12 layout (opening braces of classes and functions on their own new line, control-structure braces on the same line). Uncheck it for the K&R-style 1TBS layout used by some teams. Modern projects on PER Coding Style (the successor to PSR-12 adopted by Laravel and Symfony) keep the same brace rules as PSR-12, so the default checked state matches both standards.

What coding style standard does this PHP formatter follow?

Modern PHP code typically follows PSR-12 (Extended Coding Style) published by the PHP-FIG group, which extends the older PSR-1 and PSR-2. PSR-12 mandates 4-space indentation (no tabs), opening braces on the same line for functions and classes, one blank line after namespace and use declarations, and visibility keywords on all properties and methods. Popular formatters like PHP_CodeSniffer with PSR12 standard and PHP-CS-Fixer with @PSR12 ruleset enforce these rules. PER Coding Style (Per-CS 2.0, 2023) is the latest evolution recognized by Composer and major frameworks like Laravel and Symfony, replacing PSR-12 going forward.

Why does my PHP file fail PSR-12 validation?

Common PSR-12 violations include: tabs instead of spaces for indentation, opening brace on same line for methods (it should be on the next line for methods/functions but same line for control structures), missing visibility on class properties (public/protected/private), trailing whitespace, no newline at end of file, or `<?php` tag followed immediately by code without a blank line. Run `phpcs --standard=PSR12 file.php` or `php-cs-fixer fix --rules=@PSR12 file.php` to see exact violations and auto-fix the formatting. Both tools integrate with VSCode, PhpStorm, and CI pipelines via composer scripts.

How does PHP minification differ from JavaScript minification?

PHP minification primarily strips whitespace, comments, and removes unnecessary formatting since the code runs server-side and clients never see it. JavaScript minification additionally renames variables, hoists declarations, and applies aggressive AST transformations because the code ships to browsers and every byte matters. For PHP, OPcache compiles bytecode automatically so minified source provides almost no runtime benefit. Use minification only to obfuscate distributed libraries or reduce disk space. Tools like PHP Beautifier and Pretty Print reverse the process for debugging. RFC 7159 (JSON) and PSR-12 (PHP) explicitly recommend keeping source formatted for human readability.

What's the difference between PHP short tags and long tags?

Long tags `<?php ... ?>` are the canonical form required by PSR-1. Short tags `<? ... ?>` and short echo `<?= ... ?>` depend on the `short_open_tag` php.ini directive. PHP 5.4+ always enables `<?=` regardless of the directive, making it safe to use. Short tags `<?` are discouraged because they conflict with XML declarations (`<?xml ... ?>`) and break code portability. PSR-1 recommends only `<?php` for code files and `<?=` for templates. The closing `?>` tag is officially recommended to be omitted in pure-PHP files to avoid accidental whitespace output before HTTP headers.

PHP Formatter & Minifier — Free online PHP formatter, beautifier, minifier and PSR-12 / PER-CS compliance checker. Runs 100% in your browser, so pr
PHP Formatter & Minifier

How should I handle line length and wrapping in PHP?

PSR-12 specifies a soft limit of 120 characters and a hard limit of 80 for narrow displays — though most modern codebases use 120. Long method calls should break before the dot/arrow operator, with each chained call on its own line aligned to the receiver. Function signatures with many parameters use one parameter per line, with the opening parenthesis at the end of the function name line and the closing parenthesis on its own line aligned with the function keyword. Tools like php-cs-fixer's `method_argument_space` and `line_length_limit` rules automate this. Avoid forced reflows mid-expression which hurt readability.

What is the difference between formatting and refactoring?

Formatting changes only whitespace, indentation, and quote style — the AST (abstract syntax tree) remains identical, so behavior is unchanged. Refactoring restructures code to change names, extract methods, merge classes, or apply design patterns; the AST and runtime behavior may both change. Tools like PHP-CS-Fixer (formatter), Rector (refactor), PHPStan (static analysis), and Psalm (type-checker) cover different stages. Always run formatters in CI to keep style consistent; reserve refactors for deliberate human review with full test coverage. Mixing them in one commit makes code review extremely difficult.

How do I format heredoc and nowdoc strings correctly?

Heredoc (`<<<EOT ... EOT`) and nowdoc (`<<<'EOT' ... EOT`) are multi-line string syntaxes introduced in PHP 5.3+ and improved in PHP 7.3 with flexible closing markers and indentation support. PSR-12 requires the closing identifier on a line by itself, indented to match the surrounding code (PHP 7.3+ feature). Example: `$sql = <<<SQL\n SELECT * FROM users\n SQL;` — the indentation of the closing `SQL;` is stripped from each line of content. Heredoc interpolates variables like double-quoted strings; nowdoc does not. Use nowdoc for SQL, regex, or any string with `$` signs to prevent unintended interpolation bugs.

Should I use namespaces and how do they interact with formatting?

Yes — PSR-4 autoloading requires namespaced classes to map to file paths, and modern PHP code is unmaintainable without namespaces. The namespace declaration must be the first statement after `<?php`, followed by `use` declarations (grouped or individual), then class/function/constant definitions. PSR-12 mandates a blank line between each block: namespace, use, declaration. Order use declarations: classes first, then functions (with `use function`), then constants (`use const`), each group alphabetically sorted. Tools like `ordered_imports` in php-cs-fixer enforce this. Always use full namespaces — `\App\Models\User` — in imports for clarity.

Key Features

  • Format PHP with customizable indentation (2, 4, 8 spaces, or tabs)
  • PSR-2 style support for consistent coding standards
  • Minify PHP to reduce file size
  • Syntax highlighting for keywords, variables, functions, strings
  • Real-time statistics (characters, lines, size)
  • Copy formatted PHP to clipboard
  • Download formatted PHP as .php file
  • Upload PHP files for formatting
  • Dark mode support
  • 100% client-side processing
  • Works offline
  • Mobile-friendly design