PHP Formatter
Beautify PHP code with consistent indentation and brace placement.
How this tool works
Paste your PHP code
A full file with <?php tags, or just a code fragment.
Structure is rebuilt from braces and statements
Original whitespace and line breaks are discarded and re-derived from the code's actual structure.
Indentation follows brace depth
Each nested block gets 4 more spaces of indent, consistently throughout.
Copy the formatted result
Consistent brace placement and indentation, ready to paste back into your file.
Quick facts
- Category
- Formatters & Beautifiers
- Best for
- Cleaning up inconsistently indented PHP before a code review
About this tool
Paste PHP code to reindent it with consistent 4-space indentation and PSR-style brace placement: an opening brace stays on the same line as its statement, a closing brace gets its own line, and "} else {", "} catch (...) {", and similar continuations share a single line rather than splitting the brace onto its own line. String and comment content is left completely untouched - only the surrounding code structure is reformatted - and semicolons inside a for-loop header (like for ($i = 0; $i < 10; $i++)) are correctly recognized as part of the loop header rather than being treated as line-ending statement terminators. This is a rule-based, brace-and-statement-driven formatter, not a full PHP parser or AST-based formatter like PHP-CS-Fixer - it handles the common formatting patterns most PHP code actually uses, but unusual syntax (alternative control-structure syntax with "endif"/"endforeach", heredoc/nowdoc blocks) isn't specifically accounted for. Runs entirely client-side.
Why use this tool
Consistent brace placement
Opening braces stay with their statement; "} else {" and similar continuations share one line.
Strings and comments untouched
Only code structure is reformatted - string content and comment text are never rewritten.
Handles for-loop headers correctly
Semicolons inside for (;;) stay on one line instead of being split like statement terminators.
No setup required
Paste and get formatted code back instantly - no build step, config file, or CLI tool needed.
Frequently asked questions
No - this is a lighter, rule-based formatter that reflows code based on braces, statement terminators, and a small set of structural patterns, rather than building a full PHP abstract syntax tree. It handles the formatting patterns most everyday PHP code actually uses (functions, classes, control structures, try/catch), but it doesn't understand PHP semantics the way a real parser-based tool does, so it's better suited to quick cleanup than to enforcing a project-wide style guide.
Alternative control-structure syntax (if/endif, foreach/endforeach without braces), heredoc and nowdoc blocks, and unusual whitespace-sensitive constructs aren't specifically handled, since the formatter reasons about braces and semicolons rather than full PHP grammar. Standard brace-based code - the large majority of modern PHP - formats correctly.
No - string literals and comments are identified first and passed through completely unchanged; only the code outside of them is reformatted. This means indentation-sensitive content inside a string, or a specific formatting style in a comment block, is always preserved exactly as written.
Explore more free tools
Formatters, converters, validators, and generators - all free and running entirely in your browser.
Browse more tools