DevToolPal
EN

PHP Formatter

Beautify PHP code in PER Coding Style (PSR-12) with the official Prettier PHP plugin.

Runs locally, never uploaded

Options

Indent
Braces

Input

0 chars · 0 B

Output

0 chars · 0 B

What this tool does

The PHP formatter turns messy, minified or inconsistently indented PHP into clean code that follows the community’s coding standard. It handles modern syntax including typed and readonly properties, constructor promotion, arrow functions, enums, match expressions and attributes.

It runs Prettier with the official PHP plugin, the same combination many Laravel and Symfony teams use in their editors and pre-commit hooks, so the result matches what your tooling would produce.

How to use it

  1. Paste PHP code, or open a .php file. The opening <?php tag is optional for short snippets.
  2. The formatted code appears on the right as you type.
  3. Adjust the options:
    • Indent: 4 spaces is the PSR standard; 2 spaces and tabs are also available.
    • Line width: lines longer than this are wrapped where the syntax allows.
    • Braces: put class and function braces on the next line (PER / PSR-12) or on the same line.
    • PHP version: the oldest version the code must run on.
    • Single quotes and Trailing commas for multi-line lists.
  4. Copy or download the result.

If the code has a syntax error, the message names what the parser expected and the line and column where it stopped.

Example

The sample is a Laravel-style controller with everything crammed together: a grouped use statement, a property array, constructor promotion, a validation call, an if written on one line and an array_map with an arrow function. After formatting, each method’s brace moves to its own line, operators and => get spaces, the if body is expanded, and the long response()->json() calls are wrapped with one array item per line and trailing commas.

What it does not change

The formatter only changes layout. It does not rename variables, reorder use statements, convert array() to short syntax or fix logic. Comments, including PHPDoc blocks, are preserved and re-indented with the code around them.

Tips

  • Use the same line width and PHP version as your project’s Prettier or PHP-CS-Fixer configuration to avoid churn in diffs.
  • Formatting a snippet copied from a log or a stack trace makes it much easier to read.
  • If you share code in an issue, format it first so reviewers can focus on what it does.

FAQ

› Which coding style does it follow?

By default the output follows the PER Coding Style, the successor of PSR-12 published by PHP-FIG. Class and method braces go on their own line, control structure braces stay on the same line, and indentation is four spaces.

› Why does the PHP version matter?

Some syntax is only valid from a certain version. Trailing commas in parameter lists, for example, need PHP 8.0, so with an older target the formatter leaves them out. Pick the oldest version your code must run on.

› Can it format files that mix HTML and PHP?

Yes. Template files with inline HTML and short echo tags are supported. The PHP parts are formatted and the HTML between them is kept as it is.

› Is my code uploaded?

No. The formatter runs entirely in your browser and nothing is sent to a server.