Format Code Now
/

PHP Beautifier and Formatter

WM-77

Paste PHP to format it with consistent indentation, spacing and braces. A snippet without the opening tag works too. Errors give the line and column.

Runs in your browser. Nothing is uploaded.

PHP

Formatted PHP

How to use it

  1. Paste your PHP code into the input box, with or without the <?php tag.
  2. Pick the indent: 2 spaces, 4 spaces or a tab.
  3. Choose single quotes if you want them, and the brace style.
  4. Copy the formatted code or download it as a .php file.

What it does

The formatter is the Prettier PHP plugin. It parses the code and prints it again with consistent indentation, spacing and line breaks. Statements go on separate lines, long argument lists and arrays are wrapped, and the spacing around operators is made uniform. Comments are kept. By default braces for classes and functions go on their own line, following the PER coding style; choose 1TBS to keep them on the same line.

Snippets without the PHP tag

PHP code only counts as PHP between <?php and ?> tags. If your text contains no tag, the page adds <?php for the run and removes it from the result, so you can paste a function or a class on its own. Text outside the tags, such as HTML in a template, is not formatted and stays as written, apart from the PHP blocks inside it.

Limits

It changes layout, and quote style if you ask. It does not run the code, rename anything or enforce a coding standard beyond layout. It reads the PHP syntax the plugin supports, and newer language features can be refused with a syntax error. Errors give the line and column where parsing stopped, which can be just after the real mistake, and nothing is formatted in that case.

Error messages and what to do

MessageWhat it meansFix
Invalid PHP: syntax error, unexpected '{', expecting T_VARIABLE at line 1, column 17The parser met a { where it expected a variable, here in a function that has no parameter list.Check the code at the position shown for a missing parenthesis or variable name.

Questions people ask

How do I format PHP code online?

Paste the code and the formatted version appears as you type. Pick the indent, quotes and brace style, then copy the result or download it as a .php file. You can paste a snippet without the opening tag.

Do I need the <?php tag?

No. If the text has no <?php or <?= tag, one is added for the run and removed from the result. If you include the tag yourself, the output keeps it. Text outside the tags is left as written.

Which brace style should I pick?

Per-CS, the default, puts the opening brace of a class or function on the next line and keeps it on the same line for if and loops. 1TBS keeps all of them on the same line. Choose whichever your project's coding standard says.

Does it follow PSR-12?

The default brace layout matches the PER coding style, which continues PSR-12, and 4-space indentation is selected by default. The formatter does not check or enforce the whole standard, only the layout it prints.

Does it work on WordPress or Laravel code?

It formats any PHP that the parser accepts, so plugins, templates and framework code all work. WordPress's own style uses tabs and spaces inside parentheses, which this does not produce. Choose Tab for the indent if you want tabs.

Why do I get a syntax error?

The parser could not read the code. Typical causes are a missing semicolon, an unclosed brace or quote, or syntax newer than the plugin supports. The message gives the line and column where parsing stopped, which can be just after the actual mistake.

About this tool

Uses the Prettier PHP plugin, which reads PHP syntax and rewrites layout: indentation, spacing, line breaks and, if you ask, quote style. If your text has no opening tag, one is added for the run and removed again, so you can paste a snippet. Text outside the PHP tags is not formatted and stays as written. Comments are kept. Syntax errors are reported with their line and column and nothing is formatted. It reads the PHP 5 to 8 syntax the plugin supports, and newer syntax can be refused. It does not run the code, rename anything or check coding standards beyond layout.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.