Skip to content
ZeroServer.tools

PHP Formatter

Format and indent PHP code in your browser — no server required.

Indent
Output

Why format PHP code?

Consistent indentation in PHP makes it easier to spot unclosed braces, nested conditions, and logic errors. This formatter re-indents your code while correctly handling the cases a naive brace-counter breaks on: it tracks parenthesis depth so a for ($i = 0; $i < 10; $i++) stays on one line (rather than shattering at each semicolon), and it treats <<<EOT … EOT; heredoc/nowdoc bodies as opaque, so their braces and semicolons are preserved verbatim instead of driving the indentation. For strict PSR-12 compliance, use PHP-CS-Fixer on your server — this tool focuses on rapid readability fixes in the browser.

Built and maintained by Meet Shah · Last updated

What this tool is used for

  • Normalising indentation on a file several people have edited.
  • Making a minified or badly formatted snippet readable enough to review.
  • Reformatting a legacy file before making a change, so the diff is small.
  • Reading a fragment pasted from a forum with its line breaks lost.
  • Checking the real nesting of a long conditional block.

Frequently Asked Questions

What formatting standard does PHP use?
PSR-12, the PHP-FIG coding style, which superseded PSR-2 in 2019. It specifies four-space indentation, braces on their own line for classes and methods, and one blank line after the namespace declaration — details that are otherwise argued about indefinitely.
Why do PHP files often omit the closing tag?
Because any whitespace after `?>` is sent to the browser, and a stray newline at the end of an included file is enough to break a later `header()` call with "headers already sent". Omitting the closing tag in a pure-PHP file makes that impossible.
What is the difference between single and double quotes?
Double-quoted strings interpolate variables and escape sequences; single-quoted ones are almost literal. It matters for correctness, not only style — a password containing `$` in a double-quoted string is silently substituted with an empty variable.
How should array syntax be written?
The short form `[]`, valid since PHP 5.4 and what every current standard specifies. `array()` is the identical construct in older spelling. Multi-line arrays take a trailing comma, which keeps diffs to one line when an element is added.
Is formatting the same as linting?
No. A formatter changes layout and never changes behaviour; a linter reports problems and may not fix them. PHP-CS-Fixer and PHP_CodeSniffer straddle the line by offering rules that do both, which is why a "fix" occasionally changes more than expected.

Common errors and gotchas

  • Expecting it to enforce a coding standard, which needs a dedicated fixer rather than a formatter.
  • Reformatting inside a heredoc or a string, where whitespace is data.
  • Assuming it validates, when a syntax error formats and still fails to run.
  • Losing alignment that was deliberate, such as a lined-up array of constants.
  • Reformatting a file the project's own tooling will reformat differently, producing churn.

Related Formatters & Validators tools

Private & free — this tool runs entirely in your browser.

IndieKitShip your Next.js startup in days.affiliate