PHP Formatter

Re-indent PHP code by brace depth.

Local Your input is processed locally in your browser. Found a problem? Report an issue

What the PHP Formatter does

The PHP Formatter re-indents PHP code: each line is placed four spaces in for every unclosed {, ( or [ before it. It tidies code whose indentation was lost when it was pasted or copied from a chat or a PDF.

It reads PHP strings, character literals, comments and heredoc and nowdoc strings, # comments and #[attributes], so a brace inside them does not change the indentation, and lines inside a multi-line string are left exactly as written. It does not rewrap lines or change spacing within a line.

PHP Formatter examples

The built-in sample

Input

<?php
class Greeter {
public function greet(int $n): string {
if ($n > 1) {
return "Hello all";
}
return "Hello " . $this->name;
}
}

Output

<?php
class Greeter {
    public function greet(int $n): string {
        if ($n > 1) {
            return "Hello all";
        }
        return "Hello " . $this->name;
    }
}

This is the input that the Load sample button fills in.

Braces inside strings and comments

Input

<?php
#[Route('/')]
function page(): string {
# render {
$html = <<<HTML
  <div>{$title}</div>
HTML;
return $html;
}

Output

<?php
#[Route('/')]
function page(): string {
    # render {
    $html = <<<HTML
  <div>{$title}</div>
HTML;
    return $html;
}

The braces inside strings and comments are ignored, so the following lines are indented correctly.

Each output was produced by running this tool on the input above it.

How it works

Each line is trimmed and indented by the number of brackets ({, ( and [) still open before it. A line that starts with a closing bracket moves out one level first, so } else { lines up with its if.

While reading, the tool skips over strings and comments using the PHP rules. Lines that begin inside a multi-line string are copied unchanged, because changing them would change the string. Lines inside a block comment are re-indented, and a leading * is lined up under the opening /*. Runs of blank lines become one.

Details and limits

  • Your line breaks are kept: code that is all on one line comes back on one line.
  • The tool does not check syntax. An unclosed string or bracket shifts the lines after it, which is often a quick way to find one.
  • For a full reformat, with line wrapping and spacing rules, use PHP-CS-Fixer or your IDE.

Common errors

SymptomWhat it means
Everything after a line is indented too farA bracket or string on that line is not closed. Check the line where the indentation starts to drift.

Questions

Is my data uploaded when I use PHP Formatter?

No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.

What does PHP Formatter do?

The PHP Formatter re-indents PHP code: each line is placed four spaces in for every unclosed {, ( or [ before it. It tidies code whose indentation was lost when it was pasted or copied from a chat or a PDF.

How does PHP Formatter work?

Each line is trimmed and indented by the number of brackets ({, ( and [) still open before it. A line that starts with a closing bracket moves out one level first, so } else { lines up with its if.

Are there any limits I should know about?

Your line breaks are kept: code that is all on one line comes back on one line. The tool does not check syntax. An unclosed string or bracket shifts the lines after it, which is often a quick way to find one.

Is there a size limit?

There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.

Does it work on phones, and is it free?

Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.