PHP Formatter

Paste PHP code and get back a cleanly formatted version with consistent indentation and spacing, powered by Prettier's PHP plugin, a pure-JavaScript PHP parser and printer. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-24

Overview

Introduction

PHP written across different editors or IDEs rarely shares the same indentation and spacing conventions.

This tool reformats PHP into a single consistent style entirely in your browser, using a JavaScript PHP parser rather than a server-side PHP process.

What Is PHP Formatter?

A PHP formatter built on the Prettier PHP plugin, which parses PHP with a pure-JS parser and re-prints it with consistent spacing and indentation.

It supports standard PHP syntax including classes, namespaces, and closures.

How PHP Formatter Works

The plugin parses your PHP source into a syntax tree and re-prints it using Prettier's formatting rules.

A syntax error in the input causes parsing to fail with the reported line and column.

When To Use PHP Formatter

Use it when cleaning up PHP pasted from a legacy file or a teammate's differently configured editor.

It's a fast way to check that a PHP snippet parses without needing a local PHP install.

Often used alongside Ruby Formatter and JavaScript Validator.

Features

Advantages

  • Runs entirely client-side, with no PHP interpreter required.
  • Uses the same plugin many PHP projects use via the Prettier CLI.
  • Handles files that mix inline HTML and PHP tags, not just pure `<?php ... ?>` blocks.

Limitations

  • Only reformats whitespace and style; it doesn't lint or fix logic issues.
  • Very large files may take a moment to parse.

Examples

Inconsistently spaced function

Input

function greet($name){
  return "Hello, ".$name;
}

Output

function greet($name)
{
    return "Hello, " . $name;
}

PHP's brace-on-new-line convention and consistent spacing around concatenation are applied automatically.

Best Practices & Notes

Best Practices

  • Format legacy PHP files before a refactor so the diff reflects logic changes, not style drift.
  • Include the `<?php` opening tag even for a short snippet, so the parser recognizes it as PHP rather than literal HTML.

Developer Notes

Formatting is delegated to Prettier's `format()` API with the `php` parser, loaded from `@prettier/plugin-php`'s browser-safe standalone build.

PHP Formatter Use Cases

  • Cleaning up inherited PHP code before editing it
  • Normalizing a PHP snippet copied from documentation
  • Formatting a PHP snippet from an LLM response before adding it to a project

Common Mistakes

  • Pasting a fragment without the opening `<?php` tag when the surrounding context expects it.
  • Expecting it to modernize deprecated syntax or apply coding-standard rules; it only restyles whitespace and brace placement.

Tips

  • If formatting fails, the reported line and column point at the exact syntax problem.
  • If a snippet fails to parse, check first whether it's missing the `<?php` opening tag before assuming there's a real syntax error.

References

Frequently Asked Questions