PHP Formatter
Rescale your PHP code's indentation to a consistent width, live as you type.
<?php
function greet($name) {
if ($name) {
echo "Hello, " . $name;
} else {
echo "Hello!";
}
}Honest about what this does: it rescales your existing indentation to a consistent width — it doesn't parse PHP, so it can't fix code that isn't already consistently nested. For a full PSR-12 formatter, use a tool like PHP-CS-Fixer locally. Runs entirely in your browser.
Need the opposite? Python Formatter
What is PHP Formatter?
Paste PHP code with inconsistent indentation and this tool re-expresses it at a consistent 2 or 4 spaces, preserving the nesting already present. Honest about its limits: this doesn't parse PHP, so for full PSR-12 formatting, use a tool like PHP-CS-Fixer locally.
How to Use the PHP Formatter
- 1
Paste the PHP Code: Enter PHP with inconsistent indentation.
- 2
Choose Indent Width: Pick 2 or 4 spaces.
- 3
Read the Rescaled Result Instantly: The re-indented code appears automatically as you type.
- 4
Copy the Result: Use the copy button to copy the result.
How It Works
This tool doesn't parse PHP into an AST the way a real formatter would. Instead it tracks brace and bracket nesting depth as it scans through the code line by line, then re-indents every line to your chosen width (2 or 4 spaces) based on that depth. The nesting structure your code already has is preserved exactly; only the width of each indent step changes.
Example
<?php
function greet($name) {
if ($name) {
echo "Hi $name";
}
}<?php
function greet($name) {
if ($name) {
echo "Hi $name";
}
}The inconsistent 1- and 5-space indents are rescaled to a uniform 4 spaces per nesting level.
Common Use Cases
Quickly cleaning up a snippet pasted from Slack, an old ticket, or Stack Overflow so it's readable in a code review, without installing PHP-CS-Fixer or setting up a project just to reformat a few lines.
Frequently Asked Questions
Common questions about PHP Formatter.