Logo

MonoCalc

/

PHP Prettier

Programming
Runs entirely in your browser
Your source is tokenised and re-printed locally. Nothing is uploaded, logged or sent to a server, so proprietary and credential-bearing files are safe to paste.
PSR-12

The current PHP-FIG coding standard: 4 spaces, declaration braces on their own line, control braces on the same line, no trailing ?>.

class Invoice
{
    public function total(): float
    {
        if ($this->paid) {
            return 0.0;
        }
    }
}

Load a sample:

Valid PHP - formatted
11 lines differ from the original.
The longest input line was 152 characters; only comma-separated lists are re-wrapped, so long expressions and string literals stay on one line.

PHP source

Formatted PHP

Code statistics

Total lines

27

Code lines

24

Comment lines

1

Blank lines

2

Classes

1

Interfaces

0

Traits

0

Enums

0

Functions

1

Longest line

81 ch

Max nesting

4

Avg lines / function

24

Size: 568 B 764 B (34.5% larger)

Minimum PHP version detected: 5.4

5.4 - Short array syntax `[...]` was introduced in PHP 5.4.

Rules applied

Every rule that changed something, and why it exists.

Converted array() to []

×1

The short array syntax has been the idiomatic form since PHP 5.4 and is what PSR-12 code uses.

Wrapped over-long lists

×1

Argument lists and array literals that would run past the print width were broken onto one element per line.

Added trailing commas

×1

A trailing comma on a multi-line list keeps future diffs to one line. PHP has allowed it in arrays since 5.x and in calls since 8.0.

Re-indented lines

×14

Every line was re-emitted at the nesting depth implied by its braces, brackets and alternative-syntax blocks.

Reproduce this style in your project

{
  "parser": "php",
  "plugins": [
    "@prettier/plugin-php"
  ],
  "tabWidth": 4,
  "useTabs": false,
  "printWidth": 120,
  "singleQuote": false,
  "trailingComma": "all",
  "braceStyle": "psr-2",
  "phpVersion": "8.1",
  "endOfLine": "lf",
  "monocalc": {
    "arraySyntax": "short",
    "alignArrows": false,
    "maxBlankLines": 1,
    "spaceAroundOperators": true,
    "sortUseImports": false,
    "stripComments": false,
    "preserveDocblocks": true,
    "declareStrictTypes": false,
    "closeTag": "omit",
    "formatEmbeddedHtml": true
  }
}

About This Tool

PHP Prettier: format, beautify and minify PHP in your browser

Inherited PHP is rarely tidy. A file that has passed through a template generator, three developers and a Stack Overflow answer usually carries mixed tabs and spaces, braces in two different places, ragged array alignment and lines running past 200 characters. This PHP formatter takes that source, tokenises it, and re-prints it as clean, standards-compliant code — without ever sending a byte to a server.

Nothing leaves your machine
Most online PHP beautifiers POST your source to a remote endpoint. This one is JavaScript running in your own tab, which is what makes it usable on proprietary code, config files and anything holding credentials.

How the formatter decides where the lines go

The tool runs a tokenise → normalise → print pipeline. First the source is split into tokens: inline HTML, <?php and <?= tags, single- and double-quoted strings, heredoc and nowdoc bodies, comments, numbers, variables, identifiers and operators. Then the enabled normalisations run — array() to [], quote style, import sorting, declare(strict_types=1) insertion. Finally an indentation-aware printer emits the tokens, opening a level on { and on an alternative-syntax :, closing it before the matching } or endforeach;, and re-attaching each comment to the statement it annotates.

The critical property is that whitespace between tokens is the only thing that changes. String literals, heredoc bodies and comment text are opaque to the printer, so a formatted file computes exactly what the original did.

PSR-12, PSR-2, 1TBS and Allman

PSR-12 is the current PHP-FIG standard and the default here. Its most-noticed rule is asymmetric brace placement: the opening brace of a class, interface, trait, enum or named function goes on its own line, while the brace of an if, foreach or while stays on the statement line. The reasoning is that a declaration is a heading — giving its brace a line of its own makes the signature, which may span several lines of parameters, visually separate from the body. A control structure is a single short line, so the extra break would only add noise.

PSR-2 is the older standard PSR-12 superseded; it shares the brace rules but assumes an 80-character soft limit and says nothing about the closing tag. 1TBS (the style Prettier's PHP plugin prints) keeps every opening brace on the same line, and Allman gives every brace its own line. Any of them is defensible; what matters for a codebase is picking one and applying it everywhere, which is what the preset picker is for.

Beautify, minify and diff-check

Beautify is the default: re-indent, re-space and re-wrap. Minify runs the same token stream in reverse, dropping comments and emitting each token with the minimum separator needed to keep the file parseable, then reports the byte saving. Diff check formats the file and shows a line-level diff against the original — the fastest way to tell a reviewer “this commit is whitespace only”, or to confirm that a file already matches your house style before you touch it.

Templates, long lines and the limits

Mixed HTML and PHP view files are handled as a single indent stream: block-level markup opens and closes indent levels, alternative syntax such as foreach: / endforeach; does the same, and short echo tags stay inline with the text around them. A <li><?= $item->name ?></li> stays on one line; the <ul> wrapping it does not.

Long lines are not always re-wrapped
Wrapping happens only where a comma-separated list — an argument list, a parameter list or an array literal — would run past the print width. A single long boolean expression, a long string or a deep method chain has no break point that is guaranteed safe, so it is left intact rather than guessed at.

The tool also reports the minimum PHP version your syntax requires — match and ?-> mean 8.0, enum and readonly mean 8.1, fn() means 7.4 — alongside line counts, declaration counts, the longest line and the deepest nesting level, so an unfamiliar file can be sized up before you start editing it. When the source will not tokenise, you get the message, line and column instead of output: broken input never produces half-formatted code.

Frequently Asked Questions

Is the PHP Prettier free?

Yes, PHP Prettier is totally free :)

Can I use the PHP Prettier offline?

Yes, you can install the webapp as PWA.

Is it safe to use PHP Prettier?

Yes, any data related to PHP Prettier only stored in your browser (if storage required). You can simply clear browser cache to clear all the stored data. We do not store any data on server.

How does this PHP formatter work?

Your code is tokenised in the browser into strings, comments, heredocs, tags and operators, then re-printed at the nesting depth its braces, brackets and alternative-syntax blocks imply. Because only the whitespace between tokens changes, every string, heredoc body and comment comes back byte-for-byte identical.

Is my code uploaded anywhere?

No. The formatter is plain JavaScript running inside your own browser tab - there is no server round-trip, no logging and no analytics on the code you paste. That makes it safe for proprietary source and files that happen to contain credentials.

What is the difference between PSR-12 and 1TBS brace placement?

PSR-12 puts the opening brace of a class, interface, trait, enum or named function on its own line, but keeps the brace of a control structure such as if or foreach on the same line. 1TBS (and Prettier's PHP plugin) keeps every opening brace on the same line. Both are internally consistent - PSR-12 is what most PHP linters check against.

Can it break my code?

The formatter never rewrites the inside of a literal, and it refuses to emit anything at all when the input does not tokenise - you get the error line and column instead of half-formatted output. The optional rewrites (array() to [], quote normalisation) are deliberately conservative and skip any string containing a variable, brace or backslash. Still, review the diff before committing, and keep your tests as the final check.

Why did a long line stay long?

Lines are re-wrapped only where a comma-separated list - an argument list, a parameter list or an array literal - runs past the print width. A single long expression, a long string literal or a deep method chain has no safe break point that preserves meaning, so it is left intact rather than guessed at.

Does it handle mixed HTML and PHP templates?

Yes. Inline HTML is re-indented as a block, short echo tags stay inline with the surrounding markup, and alternative syntax such as foreach: / endforeach; increases and decreases the indent of the HTML nested inside it. Text and attribute values inside the HTML are never rewritten.