A PHP formatter for PSR-12 and PER style
This PHP formatter runs Prettier with the official PHP plugin (@prettier/plugin-php), configured for the PER Coding Style brace layout and PHP 8.3 syntax. PER Coding Style is maintained by PHP-FIG as the evolving successor to PSR-12, and most frameworks (Laravel, Symfony, and others) follow it or something very close. The plugin parses your code into a syntax tree, so modern syntax such as enums, readonly properties, match expressions and first-class callables is handled correctly.
Example
<?php
namespace App\Http;
use App\Models\User;
class UserController{public function show(int $id):array{$user=User::findOrFail($id);if($user->isAdmin()&&!$user->isSuspended()){return ['name'=>$user->name,'role'=>'admin'];}return ['name'=>$user->name];}}
<?php
namespace App\Http;
use App\Models\User;
class UserController
{
public function show(int $id): array
{
$user = User::findOrFail($id);
if ($user->isAdmin() && !$user->isSuspended()) {
return ["name" => $user->name, "role" => "admin"];
}
return ["name" => $user->name];
}
}
Class and method opening braces go on their own line, while control structures such as if keep the brace on the same line: the defining rule of PSR-12 and PER. The return type colon is followed by a space, binary operators get spaces, and ! stays attached to its operand.
What else the formatter changes
- Short arrays:
array(1, 2)becomes[1, 2]. - Quotes: plain single-quoted strings are printed with double quotes (Prettier's default), but only when the content would not change meaning.
'$x and \n'keeps its single quotes because in double quotes$xwould be interpolated. - Long expressions are wrapped at the line width: arguments one per line, chained method calls broken before
->, and??or ternaries split across lines. - Trailing commas are added to multi-line arrays and argument lists, which PHP 8 allows.
- Blank lines you leave between statements are kept (collapsed to one), but none are added. PSR-12 asks for a blank line after the
namespacedeclaration and after theuseblock, so leave them in your source. - Mixed HTML and PHP: templates with
?>and<?=blocks are supported; the HTML part is left as written.
PHP 8 syntax support
Enums with backed values, constructor property promotion, readonly properties and classes, match, named arguments, the nullsafe operator ?->, attributes (#[Route("/users")]), union and intersection types, arrow functions (fn($x) => $x * 2), never return types and throw as an expression are all formatted.
Options
Indent defaults to 4 spaces, which PSR-12 and PER require. Line width defaults to 100; PSR-12 sets a soft limit of 120 characters and recommends 80 or fewer, so 100 sits comfortably in between.
Common PHP syntax errors
- "syntax error, unexpected '}'" – usually a missing semicolon on the line before.
- Unexpected end of file – an unclosed brace or string. The formatter reports the position where parsing stopped.
- Code without
<?phpis treated as inline HTML and left untouched. Add the opening tag to format it. - A unary operator in front of
**– an expression such as-$a ** 2is refused with a message asking for parentheses, because reprinting it could change its result. Write-($a ** 2)or(-$a) ** 2first.
Prettier vs. PHP-CS-Fixer and PHP_CodeSniffer
PHP-CS-Fixer and PHP_CodeSniffer apply many rule-based fixes, such as ordering imports or converting is_null($x) to $x === null. Prettier focuses on layout and produces the same output for the same code every time. Many teams use Prettier for formatting and a linter for the rest; this page is a quick way to format a snippet or check the style before committing. To turn HTML into PHP echo statements, see HTML to PHP, and to generate typed PHP classes from JSON, try JSON to PHP. Your code is formatted in your browser and is not uploaded.