PHP Code Formatter (PSR-12 and PER Coding Style)

Format PHP files and HTML templates to PSR-12 or PER-CS 2.0, sort use statements, lower-case keywords and align => in arrays, with a diff for every file.

Funziona nel tuo browser. I tuoi file non lasciano mai il dispositivo. Scopri come verificarlo

…

Come si usa

  1. 1
    Add PHP files

    Paste code, open .php or .phtml files, or load the samples.

  2. 2
    Choose PSR-12 or PER-CS

    Pick a preset or the aligned arrays variant.

  3. 3
    Adjust options

    Set indent, brace style, keyword casing, elseif and use sorting.

  4. 4
    Check the diff

    Compare the original and formatted code line by line.

  5. 5
    Read the PSR notes

    Fix var properties, short open tags and long lines from the issues tab.

  6. 6
    Save the result

    Apply it, download the file or a ZIP, or copy the formatted code.

Informazioni su questo strumento

PSR-12 is the coding style published by the PHP Framework Interop Group, and PER Coding Style 2.0 is its maintained successor. Both ask for four-space indentation, opening braces of classes and methods on their own line, braces of if, foreach and other control structures on the same line, one space after control keywords, lower-case keywords and constants, and elseif instead of else if. This formatter applies those rules in your browser. Its tokenizer understands everything that makes PHP awkward to format with a generic tool: text outside PHP tags, short echo tags, double-quoted strings with {$array['key']} parts, heredoc and nowdoc blocks, # comments next to #[attributes], namespaced names with backslashes, and the -> and ?-> operators. When a file contains only PHP, it is fully re-indented and the closing ?> tag is dropped as PSR-12 asks. When a file mixes HTML and PHP, such as a view template using foreach (...): and endforeach, the indentation of every line is kept and only the PHP inside the tags is re-spaced, so the HTML layout never breaks. Use statements are sorted into separate blocks for classes, functions and constants, and the header blocks after the opening tag are separated by single blank lines. An optional setting aligns the => arrows in multi-line arrays, and PER-CS adds trailing commas to multi-line lists.

PSR checks

The issue list reports what PSR-1 and PSR-12 say should not be there: the short <? open tag, the var keyword used to declare a property, tab indentation, a missing declare(strict_types=1) in a PHP-only file, and lines over the 120-character soft limit. Every keyword or constant written in upper case is counted, and every else if that was rewritten is listed with its line number.

Worked example

The sample InvoiceController.php has 25 lines. With the PSR-12 preset the result has 28 lines, with 12 changed, 6 added and 3 removed. A blank line is added after the opening tag, after the namespace line and after each use block, and the three class imports are sorted with use function App\Support\money in its own block below them. The class and method braces move to their own lines, ": array" loses its space before the colon, NULL becomes null, "else if" becomes elseif, and "(int)$request" becomes "(int) $request". The closing ?> at the end is removed. The checker leaves one warning, for "var $perPage", and four notes, one of them suggesting declare(strict_types=1). The interpolated string "Total: {$totals['sum']} for {$q}" is untouched.

Limits

The built-in engine re-indents and re-spaces lines but does not split long lines or expand a one-line if block into several lines. In template mode it does not re-indent the HTML at all, by design. If you need long lines split automatically, run the project's own formatter, such as PHP-CS-Fixer, before committing. Formatting is not a syntax check, so code that PHP would reject can still be formatted.

Esempio svolto

InvoiceController.php with PSR-12: 25 lines become 28, NULL to null, else if to elseif, ?> removed, use statements in class and function blocks.

In breve

Domande frequenti

What is the difference between PSR-12 and PER Coding Style?

PER Coding Style 2.0 builds on PSR-12 and covers newer syntax such as enums and short closures. In this tool the PER-CS preset also adds trailing commas to multi-line argument lists and arrays.

Can it format files that mix HTML and PHP?

Yes. When text outside PHP tags is found, each line keeps its own indentation and only the PHP code inside the tags is re-spaced, so templates keep their layout.

Does it change the content of my strings?

No. Single and double-quoted strings, interpolated parts, heredoc, nowdoc and comments are copied exactly as they are.

How are use statements ordered?

Class imports first, then use function, then use const, each block sorted alphabetically and separated by one blank line, as PSR-12 section 3 describes.

Can I align the => arrows in arrays?

Yes. Turn on Align => in multi-line arrays or pick the Aligned arrays preset. Keys at the same level are padded so the arrows line up.

Is anything sent to a server?

No. The PHP formatter runs entirely in the page and nothing is downloaded or uploaded while it works.

Completamente gratisNessun costo, nessuna registrazione
I file restano privatiTutto funziona nel tuo browser
Facile nella tua linguaDisponibile in molte lingue