Home › Developer Tools › PHP Formatter

PHP Formatter

Paste PHP and get clean, consistently formatted code: Prettier's PHP plugin rewrites spacing, line breaks and indentation in a PSR-12 compatible style. Prefer to keep your line breaks? Switch to Re-indent only. Everything runs in your browser.

Last updated:

Loading the PHP formatter…

Tip: press Ctrl + Enter to format; Tab indents inside the editor (press Esc first to move focus). Your code is formatted in your browser and never uploaded or executed.

Advertisement
Advertisement

How to use the PHP Formatter

  1. Paste your PHP into the input, open a .php file, or load a sample.
  2. Choose the mode: Full format rewrites the code with Prettier PHP; Re-indent only fixes indentation and blank lines but keeps your line breaks.
  3. Set the style: indentation, brace style, print width, quotes, trailing commas and target PHP version.
  4. Press Format PHP (or Ctrl + Enter). If the code has a syntax error, the line and column are shown and highlighted.
  5. Copy or download the formatted code, or press Use output as input to keep working on it.

PSR-12 layout rules applied by the formatter

RuleExample
4 spaces per indentation level, no tabs    return $total;
Class, interface, trait and method opening brace on its own linefinal class Cart
{
Control-structure brace on the same line, one space after the keywordif ($a === $b) {
One space around binary operators and after commas$x = $a + $b * 2;
Return types: no space before the colon, one space after itpublic function total(): int
One statement per line; one blank line after the namespace and use blocksnamespace App;

use App\Model\User;
Arguments split one per line when the call is longer than the print width$this->mailer->send(
    $to, …

Brace styles compared

PSR-12 / PER places the opening brace of classes, functions and methods on a new line, but keeps braces of if, foreach, while and try on the same line. 1TBS (the "one true brace style") puts every opening brace on the same line, which some teams prefer for consistency with JavaScript code. Both are applied consistently across the file, so pick the one your project's coding standard (or PHP_CodeSniffer / PHP-CS-Fixer config) uses.

Common PHP syntax errors

  • syntax error, unexpected end of file: a { is never closed; the error is reported at the end, so look upward for the missing }.
  • unexpected identifier / variable: usually a missing semicolon at the end of the previous line.
  • unexpected string content: a quote is missing, or a variable is used inside single quotes where it is not interpolated.
  • Heredoc not closed: before PHP 7.3 the closing identifier had to start at column 0; later versions allow indentation but it must match the body.

Formatting templates with lots of markup? Tidy the HTML parts with the HTML Formatter, and the queries with the SQL Formatter.

Frequently asked questions

Is my PHP code uploaded to a server?

No. The formatter is JavaScript (Prettier and its PHP plugin, which includes a PHP parser written in JavaScript) running in your browser. Your code is never sent anywhere and never executed.

Does the output follow PSR-12?

The default settings produce code that follows PSR-12 and its successor, the PER Coding Style, for layout: 4-space indentation, opening braces of classes and methods on their own line, control-structure braces on the same line, and one statement per line. Naming rules and other non-layout parts of PSR-12 are not changed by a formatter.

What is the difference between Full format and Re-indent only?

Full format parses the file and prints it again from scratch, so spacing, line breaks, quotes and long lines are all normalized. Re-indent only keeps every line as you wrote it and fixes the indentation according to brackets, switch/case blocks and continuation lines, which is useful for legacy templates or when you want minimal diffs.

Why does it say there is a syntax error?

Full format needs code that PHP can parse: missing semicolons, unclosed brackets or strings, and mismatched quotes are reported with the line and column. Re-indent only does not need valid code, but it warns about brackets or strings that are never closed.

Can it format PHP mixed with HTML?

Yes. Inline HTML outside the <?php ?> tags is kept as is, while the PHP blocks are formatted. For heavy templates, Re-indent only usually gives the most natural result.

Which PHP versions are supported?

Modern syntax up to PHP 8.3 is understood, including enums, readonly properties, match expressions, named arguments, attributes, arrow functions, nullsafe ?-> calls and first-class callable syntax. Pick a PHP version to stop the formatter from using syntax (such as trailing commas in parameter lists) that an older version would reject.

Are all the tools on FreeToolCabin tested for quality and accuracy?

Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.

Related tools

HTML FormatterBeautify and indent HTML code with a live preview.SQL Formatter & BeautifierFormat, beautify or minify SQL queries for MySQL, PostgreSQL and more.JavaScript BeautifierFormat and beautify messy or minified JavaScript, or minify it.Code Diff CheckerCompare two code files side by side or unified, with syntax colors and changes highlighted.WordPress Salt GeneratorGenerate fresh WordPress security keys and salts for wp-config.php, .env or Docker in one click.JSON FormatterBeautify, minify and validate JSON, with a collapsible tree view.CSS FormatterBeautify CSS with indent controls and optional property sorting.XML FormatterBeautify, validate and minify XML with a collapsible tree view.ZIP File CreatorCompress files and folders into a ZIP.YAML Validator & FormatterValidate, format and minify YAML, and convert YAML to JSON or back.HTML MinifierMinify HTML: strip comments and whitespace safely and see how many bytes you save.CSS MinifierMinify CSS with csso or a built-in engine and see the size saved instantly.JavaScript MinifierMinify JavaScript with Terser: compress, mangle and see how many bytes you save.JSON MinifierMinify and validate JSON, sort keys and see exactly how many bytes you save.IPv6 Expander & CompressorExpand IPv6 addresses to full form or compress them per RFC 5952, one or many at once.GraphQL FormatterPretty-print and indent GraphQL queries, mutations and schemas, or minify them, instantly.CSS ValidatorValidate CSS: syntax errors, unknown properties and invalid values by line, plus auto-fix.Gzip Compress / DecompressGzip or deflate text and files, get Base64 output, and decompress Base64 or .gz files.IBAN ValidatorCheck an IBAN's mod-97 checksum, country and length, and see its bank code and account parts.JavaScript Syntax CheckerCheck JavaScript for syntax errors with line and column, for any ES version, script or module.JSON Schema ValidatorValidate JSON against a JSON Schema (draft-07, 2019-09, 2020-12) with clickable error paths.Markdown FormatterBeautify Markdown with Prettier: list markers, prose wrap, aligned tables and live preview.TypeScript FormatterFormat TypeScript and TSX code with Prettier: width, tabs, semicolons and quotes options.XML MinifierMinify and validate XML: strip whitespace and comments, keep CDATA, see the size savings.C# FormatterBeautify C# code online: Allman or K&R braces, indent size and line width, clang-format option.HTML ValidatorCheck HTML for errors and warnings with line numbers, locally or with the W3C Nu checker.Java FormatterBeautify Java code online: indent size, brace style and line width, with clang-format option.JavaScript ObfuscatorObfuscate JavaScript with string encoding, renaming and control-flow flattening presets.Phone Number FormatterFormat and validate lists of phone numbers to E.164, international or national style.Python FormatterFormat Python code in Black/Ruff style: line length, quote style, indent, copy or download.Responsive Design CheckerPreview any website at phone, tablet, laptop and desktop sizes side by side, with rotate and zoom.Browser Compatibility CheckerSearch CSS, JavaScript and HTML features and see which browser versions support them.Core Web Vitals CheckerCheck real-user LCP, INP and CLS for any URL or site and see if it passes Core Web Vitals.Heading Structure CheckerPaste HTML to see its H1-H6 outline and catch missing H1s, multiple H1s and skipped levels.Website Hosting CheckerFind out who hosts a website: hosting provider, IP addresses, ASN, network owner and location.Routing Number ValidatorCheck a US ABA routing number's checksum and format and see its Federal Reserve district.Spam Word CheckerScan an email subject and body for spam trigger words, excess caps, exclamation marks and risky links.Check Digit CalculatorCalculate or validate check digits for UPC, EAN, GTIN, SSCC, ISBN, ISSN and mod 11 codes.Luhn Algorithm CheckerValidate numbers with the Luhn mod 10 check or calculate the missing check digit, step by step.XSD ValidatorValidate XML against an XSD schema in your browser and see each error with its line number.Strftime Format BuilderBuild Python and C strftime format strings by clicking tokens, with a live preview and reference.
See all 41 Code Formatters & Validators →