How to use PHP Formatter
- Paste your PHP, including the opening tag.
- Read the formatted output.
- Copy it back, or set up PHP-CS-Fixer for anything you maintain.
- Formatting changes whitespace only — never behaviour.
- Omit the closing ?> in files containing only PHP; trailing whitespace after it breaks headers.
- Your code stays in the browser and is not transmitted.
PSR-12 is the standard worth following
PHP-FIG’s PSR-12 settles the questions teams otherwise argue about: four spaces rather than tabs, opening brace on the same line for control structures and on its own line for classes and methods, one space after control keywords, visibility declared on every property and method. Practically every modern PHP project follows it, which means code written to it reads as familiar to anyone joining. It superseded PSR-2, which you will still see referenced in older documentation.
The closing tag you should omit
In a file containing only PHP, leave off the closing ?>. Any whitespace or newline after it is sent to the browser as output, and that is the classic cause of “headers already sent” errors — a trailing blank line in an included file, invisible in the editor, breaking every redirect and cookie on the site. Omitting the tag makes the whole class of bug impossible. PSR-12 requires it.
Use a real tool in a real project
PHP-CS-Fixer and PHP_CodeSniffer with the PSR-12 ruleset do this properly, understand PHP’s syntax, and can be run in a pre-commit hook and in CI so formatting never appears in a diff. A web formatter is for a snippet you are reading. The value of automatic formatting is not tidiness — it is that code review stops containing style arguments and starts being about the code.
Frequently asked questions
What PHP standards are used?
Can it format PHP mixed with HTML?
Does it check PHP syntax?
What is PSR-12?
Should PHP files end with a closing tag?
Does formatting affect performance?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.