Bytelify

PHP Formatter

Rescale your PHP code's indentation to a consistent width, live as you type.

Runs in your browser, nothing sent
php-formatter
Reindented
<?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. 1

    Paste the PHP Code: Enter PHP with inconsistent indentation.

  2. 2

    Choose Indent Width: Pick 2 or 4 spaces.

  3. 3

    Read the Rescaled Result Instantly: The re-indented code appears automatically as you type.

  4. 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

Before
<?php
function greet($name) {
 if ($name) {
     echo "Hi $name";
   }
}
After
<?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.

FAQs

Frequently Asked Questions

Common questions about PHP Formatter.

No, it only rescales indentation width. It doesn't touch brace placement, spacing around operators, or any other style rule.

No, it doesn't parse PHP at all, so it can't detect syntax errors. It just re-indents whatever structure your code's whitespace already shows.

A real PHP AST parser is a much larger piece of software than Prettier's JS/CSS/HTML/Markdown parsers, so a full implementation wasn't bundled just for this one tool; the brace-tracking approach handles the common "my indentation is a mess" case without that dependency weight.

Yes, Bytelify's PHP Formatter is free with no account or sign-up required.

No. Formatting runs entirely client-side in your browser, so nothing you paste is sent anywhere.