PHP Formatter
Turn minified or messy PHP into clean PSR-12 style code, and see the line of any syntax error
Your data stays with you. Conversion happens inside the browser; nothing is sent to a server.
Did this tool do the job?
Thanks, your feedback came through.
How it works
PHP from a plugin or an old project often arrives squeezed onto one line, or written with a different habit in every file. Paste the code into the left box. On the right, class and function braces move to their own line, if, foreach and while blocks open on the same line, every statement gets its own line and operators such as =, => and + get a space on each side. For example, function add($a,$b){return $a+$b;} becomes a three-line function with a 4-space indent. This layout is close to PSR-12, the shared coding style of the PHP community. If the code has a syntax error, such as a missing semicolon or an unclosed bracket, nothing is reformatted: you see the line and column instead, so you can jump straight to it in your editor. If you paste a template that mixes HTML and PHP, only the inside of the <?php ... ?> blocks is changed. The result line tells you how many lines the code grew to.
This tool is also known as php formatter, php beautifier, php code formatter online, format php code, psr-12 formatter, php pretty print.
What is PHP?
PHP is a programming language that runs on the web server and builds the page before it is sent to the visitor. WordPress, Laravel and many online shops are written in it. Code starts with the <?php tag, and the same file can also contain HTML. PHP ignores line breaks and extra spaces, which is why code squeezed onto one line still runs but is hard to read.
What is PSR-12?
PSR-12 (Extended Coding Style) is the style recommendation from the PHP Framework Interop Group and replaces the older PSR-2. It asks for a 4-space indent, class and method braces on their own line, if and foreach braces on the same line, and lines that preferably stay under 120 characters. The goal is that code written by different people looks the same.
What is the difference between a PHP formatter and a PHP validator?
A formatter changes how code looks: indentation, spaces and line breaks. A validator (linter) checks whether PHP can read the code at all. Because this tool parses the code before formatting it, it also catches syntax errors and tells you where they are. It does not find undefined variables, wrong types or errors that only appear when the code runs; for those you need to run the code or use an analysis tool such as PHPStan.
Does formatting change what the code does?
No. The tool first reads the code with a PHP parser, then prints the same statements with a new layout. Variable names, strings, comments and the order of statements stay the same. Only spaces, indentation and line breaks change, and PHP ignores those, so the code runs the same way. One addition does not change the meaning either: a one-line if body written without braces (if ($x) return 1;) gets braces, as PSR-12 requires.
If the code cannot be parsed, the tool does not guess; it stops and shows where the problem is. Reformatting half-broken code could hide the error instead of pointing at it.
What is PSR-12 and how close does this tool get?
PSR-12 is the coding style recommendation published by PHP-FIG: 4-space indent, class and method braces on a new line, control-structure braces on the same line, one space after keywords. Most Laravel, Symfony and Composer packages follow something close to it.
This page uses the Prettier PHP plugin. Its output is very close to PSR-12 but not identical: it wraps long lines at 80 characters and lays out arrays and argument lists by its own rules. If your project enforces a style in CI, run the project's own tool (PHP_CodeSniffer, PHP-CS-Fixer); this page is for making code readable quickly.
- Reading someone else's code, fixing a snippet from an email → this page
- Enforced style before every commit → your project's PHP-CS-Fixer setup
- 2 spaces only if your team agreed on it; PSR-12 asks for 4
Frequently asked questions
How do I make minified PHP code readable?
Paste the code into the left box on this page. Each statement goes onto its own line and function and class blocks are indented with 4 spaces. Copy the result or download it as a .php file.
What happens if my code has an error?
Code with a syntax error is not formatted; you see the line and column of the error instead. The cause is usually a semicolon or bracket missing on the line before.
Can I format a .php template that contains HTML?
Yes. Only the inside of the <?php ... ?> blocks is formatted; the HTML between them is left untouched. To tidy the HTML part as well, use the HTML formatter separately.
Is my code sent to a server?
No. Formatting runs entirely in your browser; the code you paste never leaves this page and is not stored anywhere.