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
Paste the code
Add your snippet to the PHP code field. Existing spacing does not matter, because it is rebuilt.
-
2
Set the Indent option
Select 2 for compact output or 4 for the usual PHP width.
-
3
Continue
Use the main button to see the indented code.
-
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
Color Wheel Explorer
Explore the colour wheel interactively. Rotate harmonies, compare HSL vs RYB, spot complementary and triadic pairs in real time.
FPS Counter
Measure browser FPS with requestAnimationFrame, smoothing, min/max frame rate, warnings and an optional graph. Runs locally with no upload or API.
Placeholder Image Generator
Create a 1–4000 px placeholder with custom colors and text, download it as PNG, or copy a URL and HTML image tag.
Random Phone Number Generator
Generate 1 to 50 fictional phone numbers from officially reserved ranges for the US, Canada, UK, Australia or Germany.
GitHub Profile README Generator
Build the README.md for your GitHub profile page (the username/username repo) with tagline, skills, stats cards and language breakdown.
Color Palette Generator
Generate monochromatic, analogous, complementary, triadic or tetradic color palettes from a base HEX color and export copy-ready CSS variables.
Tool available in other languages
- จัดการเยื้องโค้ด PHP [TH]
- Wcinanie kodu PHP [PL]
- Thụt lề mã PHP [VI]
- Indentasi Kode PHP [ID]
- إزاحة كود PHP [AR]
- PHP-Code einrücken [DE]
- PHP コードをインデントする [JA]
- Indentar código PHP [PT]
- Aplicar sangría a código PHP [ES]
- PHP 코드 들여쓰기 [KO]
- Indentera PHP-kod [SV]
- Indenter du code PHP [FR]
- PHP-code inspringen [NL]
- Indentare codice PHP [IT]
- Отступы в коде PHP [RU]
- PHP Kodunu Girintileyin [TR]
- 缩进 PHP 代码 [ZH]