Loading…

About this PHP Formatter Online PHP Beautifier

Got messy PHP code from legacy projects, quick hacks or copy-pasted snippets? This online PHP formatter runs your code through Prettier with the official PHP plugin, giving you PSR-12–style output with consistent indentation, spacing and arrays in seconds. Perfect for Laravel, Symfony, WordPress, APIs and CLIs.

Why Use This PHP Formatter?

  • PSR-12–aligned formatting for modern PHP codebases
  • Powered by Prettier + @prettier/plugin-php for stable, opinionated output
  • Options for indent size, spaces vs tabs, and wrap / line length
  • Normalizes spacing, indentation, arrays, control structures and braces
  • Works with .php, .phtml, .php5, .php4 and .inc files
  • Fast in-browser formatting with automatic fallback to a secure backend when needed
  • Great for Laravel, Symfony, WordPress, custom frameworks and legacy apps

🔧 How to Beautify PHP Code in 4 Steps for php-formatter

1

1. Paste or drop your code

Paste your PHP code into the editor, or drag-and-drop a `.php` / `.phtml` / `.inc` file. The tool auto-detects PHP syntax and prepares it for formatting.

2

2. Configure formatting options

Choose your indent size, whether to use spaces or tabs, and the preferred wrap / line length. Defaults are sensible for most PSR-12–style projects.

3

3. Click “Format”

Hit **Format** to run your code through Prettier’s PHP engine. In typical cases, the result appears almost instantly.

4

4. Review, copy, and commit

Review the formatted output, then copy it back into your editor or download it as a `.php` file. Commit the cleaned version to keep future diffs tidy.

Technical Specifications

Standards & Syntax Support

The PHP formatter is tuned for modern frameworks and libraries while keeping classic syntax working smoothly.

FeatureSupportedNotes
PSR-12 alignment✅ YesOpinionated style broadly aligned with PSR-12 and common community practices.
PHP 7+ syntax✅ YesNamespaces, scalar type hints, return types, nullable types, etc.
PHP 8+ features✅ YesAttributes, union types, constructor promotion, match expressions, arrow functions.
Short array syntax✅ YesNormalizes arrays to modern `[ ... ]` style where appropriate.
Heredoc / Nowdoc✅ YesPreserves content while cleaning surrounding indentation.
Mixed PHP + HTML✅ YesCan format typical `.php` templates containing embedded HTML.

Cleanup & Layout Rules

Prettier’s PHP engine focuses on layout only—your code’s behavior stays the same.

AreaWhat is normalized?Why it helps
IndentationConsistent indent size using spaces or tabs per your settingsRemoves hand-crafted indentation drift over time.
SpacingSpaces around operators, commas, control structures and keywordsMakes expressions and conditions easier to read.
Braces & blocksStable brace placement for classes, functions and control-flowPrevents style wars and keeps diffs predictable.
ArraysElements on multiple lines when needed, consistent trailing commas rulesImproves large configuration arrays and payloads.
Blank linesReasonable grouping between functions, methods and classesGives files a clear, readable structure.

Execution Model & Limits

Designed to be fast and safe in a browser environment.

AspectValue / BehaviorNotes
Execution enginePrettier + @prettier/plugin-phpRuns in-browser whenever the plugin is available.
FallbackSecure `/api/php-formatter` backendUsed only if the PHP plugin cannot be loaded client-side.
Timeout~25 secondsRequests are aborted if formatting takes too long.
Max input size (text)~2 MBHard guardrail in the formatter logic.
Max file size (upload)~5 MBDefined by the tool’s `maxFileSizeBytes` configuration.
For large monorepos or bulk refactors, run Prettier or PHP-CS-Fixer locally over your codebase instead of relying on any online tool.

Privacy & Security

This tool is intended for everyday development snippets rather than highly sensitive production code.

AspectBehavior
Client-side formattingPreferred path using the browser’s Prettier runtime.Avoids sending code across the network in normal conditions.
Backend fallbackIf the PHP plugin cannot be loaded, code is sent via HTTPS to a secured formatting endpoint.Processed as transient input for formatting only.
StorageNo long-term storage of raw input is intended.Only aggregated error/performance metrics may be collected.
Best practiceDo not paste secrets, access tokens or passwords into any online formatter.Keep security-sensitive code on local tools and CI.

Command-Line PHP Formatting (Prettier & PHP-CS-Fixer)

When you’re working inside a project, local tools are the best way to enforce a consistent style across the whole repo.

Node / npm

Install Prettier with PHP plugin

npm install --save-dev prettier @prettier/plugin-php

Adds Prettier and its PHP plugin to your project.

Format a single PHP file

npx prettier --write src/Example.php --parser php

Applies the same style used by this online tool.

Format all PHP files in a project

npx prettier "src/**/*.php" --parser php --write

Keeps the entire codebase consistently formatted.

PHP-CS-Fixer

Install PHP-CS-Fixer globally with Composer

composer global require friendsofphp/php-cs-fixer

Adds a powerful PHP-specific style fixer to your CLI toolkit.

Run with PSR-12 rules

php-cs-fixer fix src --rules=@PSR12

Applies PSR-12 to all PHP files under `src`.

Use this page for quick one-off cleanups, then rely on Prettier or PHP-CS-Fixer in CI and pre-commit hooks to keep your entire team on the same style.

Common Use Cases for the PHP Beautifier

Laravel / Symfony / API Backends

Tidy up controllers, services and DTOs before refactoring or code review.

  • Normalize formatting of HTTP controllers and form handlers.
  • Clean up service classes and repositories shared across teams.
  • Ensure consistent style across microservices and APIs.
// Before
class OrderController extends Controller{public function store(Request $request){$order=$this->service->create($request->all());return response()->json($order,201);}}
// After
class OrderController extends Controller
{
  public function store(Request $request)
  {
    $order = $this->service->create($request->all());

    return response()->json($order, 201);
  }
}

WordPress & Legacy Codebases

Bring older PHP 5 / 7 projects closer to modern standards without rewriting everything.

  • Beautify theme and plugin code before debugging.
  • Clean up messy, copy-pasted snippets from tutorials.
  • Gradually move legacy files toward PSR-12 conventions.

Teaching & Code Reviews

Use formatting to focus on logic and architecture rather than brace placement.

  • Show students how consistent style improves readability.
  • Attach before/after examples in pull requests to highlight improvements.
  • Use as a quick sanity check for code samples in blog posts and documentation.
// Example exercise: ask students to predict the formatted result
if($user&&$user->isAdmin()){logAction($user,"login");}

❓ Frequently Asked Questions

Does this tool support modern PHP 8 features?

Yes. The formatter supports typical modern PHP syntax, including type hints, union types, constructor promotion, attributes, match expressions and arrow functions.

🔧Can I change tab width or use tabs instead of spaces?

Absolutely. You can choose the indent size (1–8) and whether indentation uses spaces or tabs. The formatter applies those settings consistently throughout your file.

📏How is line wrapping handled?

Use the wrap / line length option to hint how wide lines should be. Setting 0 effectively disables wrapping by using a very large internal limit.

🔐Is my code uploaded to your servers?

The tool prefers in-browser formatting via Prettier. If the PHP engine cannot be loaded in your environment, it falls back to a secured API for formatting. In both cases, input is intended to be transient and not stored long-term, but you should still avoid pasting secrets or highly sensitive data.

🧮Will formatting change how my PHP code runs?

The formatter only changes layout—whitespace, indentation, line breaks and similar. It does not intentionally alter semantics. That said, always run your test suite after large automated refactors.

⚙️How do I enforce the same style in my project?

Use Prettier with the PHP plugin or PHP-CS-Fixer in your project and CI pipeline. This online tool is a convenient front-end for quick fixes; local tooling is better for enforcing style at scale.

Pro Tips

Best Practice

Add a formatting step (Prettier or PHP-CS-Fixer) to your CI pipeline so no unformatted PHP ever reaches `main`.

Best Practice

Run the formatter before opening a pull request to keep diffs focused on behavior changes, not whitespace.

Best Practice

For big refactors, format the entire project once on a dedicated branch, merge it, then do logical changes in separate PRs.

Security Tip

Treat online tools like shared terminals: avoid pasting production secrets and internal keys into any web form.

Additional Resources

Other Tools