Skip to main content

PHP Formatter

Format and beautify raw or minified PHP code with configurable indentation depth, brace placement style, and operator spacing rules applied in a single pass. A tokenizer separates string literals, single-line and block comments, heredoc blocks, and nowdoc blocks from executable code so that formatting changes never alter program logic or break quoted content.

Runs entirely in your browser

This tool sends nothing over the network. Everything you enter is processed on your device and never reaches our servers.

Text
Productivity
Loading the tool
Reference

Documentation

Formatting PHP code means tokenizing raw or minified source to separate executable code from string literals, comments, and heredoc or nowdoc blocks, then applying indentation and spacing rules to the code alone, so the result reads consistently without any change to program logic. A sample function and class definition ships with the tool for quick testing.

Indentation can use spaces or tabs, with the number of spaces per level configurable when space-based indentation is selected. Brace style determines whether an opening brace sits on the same line as its control statement, K&R style, or on the following line, Allman style. Most PHP projects place braces on the same line following PSR-12 conventions, while some codebases prefer next-line placement for visual separation.

Space after commas inserts a space after every comma in argument lists, array definitions, and parameter declarations. Space around operators adds spaces around assignment, arithmetic, comparison, and concatenation operators. Trimming trailing whitespace removes invisible spaces and tabs at the end of each line, and a maximum-consecutive-blank-lines setting collapses runs of empty lines down to a chosen limit.

The tokenizer preserves single-quoted strings, double-quoted strings with escape sequences, heredoc and nowdoc blocks, single-line comments starting with // or #, and multi-line comments wrapped in /* and */, none of which are reformatted as if they were code. Nested curly braces inside string interpolation are tracked separately to keep indentation depth correct around them, since a brace that only delimits a variable name inside a string is not a code block and must not be counted as one when the formatter decides how deeply the next line should indent. Array arrow (=>), object access (->), nullsafe (?->), scope resolution (::), and spread (...) operators keep their original meaning and are left untouched by the operator-spacing rules, unlike assignment, arithmetic, comparison, and concatenation operators, which the spacing rules do adjust.

Consistent formatting reduces cognitive load when reading code and eliminates style debates during review. The following scenarios describe practical situations where automated PHP formatting saves time and enforces standards.

  • Code Review Preparation: Format a colleague's pull request diff before reviewing to normalize indentation and spacing. Reading code that follows a single style convention makes logic errors, missing braces, and incorrect nesting levels stand out more clearly than scanning inconsistently formatted files.
  • Legacy Codebase Cleanup: Process files from older PHP projects that mix tabs with spaces, omit operator spacing, or place braces inconsistently, replacing the original content with standardized output file by file.
  • Learning and Teaching: Reformat example code from tutorials, Stack Overflow answers, or documentation before studying it. Minified or compressed snippets become readable when expanded with proper indentation, making control flow and variable scope easier to follow for students and junior developers.
  • Blog and Documentation Authoring: Prepare PHP code samples for technical articles by formatting them with a consistent indent size and brace style. Running samples through the formatter before embedding them eliminates manual spacing adjustments.
  • Rapid Prototyping: Write quick PHP scripts in a single line or with minimal formatting during prototyping, then format the result once the logic works. Separating the writing phase from the formatting phase keeps momentum during development without sacrificing readability in the committed version.
  • Configuration File Generation: Format PHP array configuration files that were generated by export scripts or build tools. Generated config files often arrive as a single long line or with irregular indentation, and reformatting them makes manual edits and version control diffs significantly more readable.
  • Cross-Team Standardization: Apply a uniform brace style and indent size when merging contributions from multiple teams that follow different internal conventions.
Inputs, outputs, and what the PHP Formatter computes

What the PHP Formatter asks for and what it returns, as a plain list. Defaults, units, and ranges are the ones the form loads with.

Inputs

  • PHP Code
  • Indentation Style · default: Spaces
  • Indent Size · default: 4
  • Brace Style · default: Same line (K&R)
  • Space after commas · default: on
  • Space around operators · default: on
  • Trim trailing whitespace · default: on
  • Maximum consecutive blank lines · default: 2
  • Formatted PHP

Controls

Reset · Copy