Indent PHP Code

Paste PHP code that has no indentation, or indentation that drifted, and get every nested block aligned again. You choose 2 or 4 spaces per level. Indentation follows curly braces, so functions, classes, loops and conditions each step in by one level.

How to indent PHP code

  1. 1

    Paste the code

    Add your snippet to the PHP code field. Existing spacing does not matter, because it is rebuilt.

  2. 2

    Set the Indent option

    Select 2 for compact output or 4 for the usual PHP width.

  3. 3

    Continue

    Use the main button to see the indented code.

  4. 4

    Copy it

    Check the nesting looks right, then copy the result into your file.

How the indentation is calculated

The tool keeps a depth counter. Each opening brace adds one level and each closing brace removes one. Every line is padded with the chosen number of spaces multiplied by the current depth. The indent is always spaces, never tabs.

Here is the same function with each setting. The input was function total($items){$sum=0;foreach($items as $item){$sum+=$item["price"];}return $sum;}.

With 4 spaces:

function total($items)
{
    $sum=0;
    foreach($items as $item)
    {
        $sum+=$item["price"];
    }
    return $sum;
}

With 2 spaces:

function total($items)
{
  $sum=0;
  foreach($items as $item)
  {
    $sum+=$item["price"];
  }
  return $sum;
}

2 spaces or 4 spaces

Four spaces is the usual convention in PHP projects, and the one most style guides ask for. Two spaces keeps deeply nested code narrower, which helps when you paste into a small ticket field or a chat message. Match whatever your team uses.

Things to know

  • Tabs in your input are not preserved, since whitespace around braces and semicolons is rebuilt.
  • Indentation depends only on braces. Code that continues over several lines inside parentheses, such as a long array, is not indented further.
  • A brace inside a comment or a heredoc is still counted, which can shift the depth for everything after it.
  • Unbalanced braces are not reported. If the closing braces run out, the depth stops at zero and later lines are not indented.

Fixing code that lost its indentation

Indentation often disappears when code goes through a form field, an email, a chat tool or a log file. Pasting it here rebuilds the structure from the braces, so you do not need to realign each line by hand. This helps most with classes, where methods and bodies sit at different depths, and with long functions that have several nested loops and conditions.

Check the result

After formatting, scan the left edge. Each closing brace should line up with the line that opened its block. If the last brace is indented, or the code after a block is pushed in too far, a brace is missing or extra in your input. Fix that first and run the tool again.

Frequently Asked Questions

No. The Indent option offers 2 or 4 spaces. Convert spaces to tabs in your editor afterwards if you need them.

Only curly braces change the depth. Square brackets and parentheses do not add a level.

Nothing is flagged. The tool simply keeps the deeper indentation until the end, so a skewed result is a hint to check your braces.

Yes. Line breaks around braces and semicolons are rebuilt, and blank lines are removed.

Related Tools

Tool available in other languages

Guides and use cases