Developer and Text Tools

PSR-12 and PER Coding Style in Practice: Formatting PHP and Templates

The PSR-12 and PER-CS rules that matter most, how they apply to classes, control structures and use statements, and how to format PHP templates safely.

Saizul Amin
Saizul Amin7 Nov 2026 · 4 min read
PSR-12 and PER Coding Style in Practice: Formatting PHP and Templates

PHP code arrives in many shapes. Some files are pure classes in a framework, others are templates where PHP and HTML alternate line by line, and older projects have a bit of both. PSR-12, published by the PHP Framework Interop Group, and its successor PER Coding Style 2.0 give all of them one shared layout. This guide covers the rules you will notice first and shows how the PHP Code Formatter applies them without breaking templates.

The file header

A PSR-12 file starts with the opening tag on its own line. After it come, in order and each separated by a single blank line, the file docblock if there is one, declare(strict_types=1), the namespace line, and the use statements. The use statements themselves are split into three blocks: classes first, then use function, then use const.

The formatter enforces the blank lines and sorts each use block alphabetically. It also reports when declare(strict_types=1) is missing, but it does not add it, because turning on strict types can change how your code behaves at run time.

Braces: two rules, not one

PSR-12 uses two brace styles in the same file. Classes, interfaces, traits, enums and methods have their opening brace on the next line. Control structures such as if, foreach, while and switch keep it on the same line, after one space. Closures also keep it on the same line, which is why the formatter checks whether a function keyword starts a declaration or an expression before it decides where the brace goes.

An else or elseif goes on the same line as the closing brace, and else if written as two words is rewritten as elseif. The issues tab lists each rewrite with its line number.

Keywords, casing and spacing

All PHP keywords and the constants true, false and null are written in lower case. Type casts take no spaces inside the parentheses and one space after, as in (int) $value. Binary operators, including the string concatenation dot, have a space on each side. A return type follows the closing parenthesis directly, as in ): array.

The closing ?> tag must be left out of files that contain only PHP, because a stray newline after it can end up in the output and break headers. The formatter removes it when it finds one at the end of such a file.

Templates are a special case

In a view file, the HTML structure matters more than the PHP indentation. If the formatter re-indented every PHP line by brace depth, it would push the HTML around and make the template harder to read. So when it finds text outside the PHP tags, it switches to template mode: every line keeps its own indentation, and only the code inside each tag is re-spaced. A line like <li><?=$row['name']?></li> becomes <li><?= $row['name'] ?></li>, and nothing else in the layout moves.

Arrays and alignment

PSR-12 does not require aligned arrows in arrays, so the default preset leaves them as single spaces. Many teams like them aligned anyway, and the Aligned arrays preset pads the keys of a multi-line array so that every => lines up under the longest key. Nested arrays are aligned on their own, so a long key in an inner array does not push the outer one.

PER Coding Style asks for trailing commas in multi-line argument lists and arrays. The PER-CS preset adds a comma after the last item when the closing bracket is on its own line.

Strings, heredoc and comments

Interpolated strings with parts like {$items['total']}, heredoc and nowdoc blocks, and comments are read as whole tokens and copied unchanged, including their spacing. Even a # comment next to a PHP 8 #[Attribute] is told apart correctly, because the tokenizer checks the character after the hash.

A sensible workflow

Open the files you want to tidy, choose PSR-12 or PER-CS, and read the diff. Fix the warnings that a formatter cannot fix for you, such as properties declared with var or short <? open tags. Then apply the formatting and download the files, a ZIP of all of them, or a patch.

The built-in engine keeps your line breaks and does not wrap long lines. If you want full reprinting with wrapped lines, run your project's own formatter, such as PHP-CS-Fixer, as a final step.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages