ModernCalcs

PHP Beautifier

Indent
<?php

interface Cacheable {
    public function getCacheKey(): string;
    public function getTtl(): int;
}

class UserService implements Cacheable {
    private array $cache = [];
    private string $prefix;

    public function __construct(string $prefix = 'user') {
        $this->prefix = $prefix;
    }

    public function getCacheKey(): string {
        return $this->prefix . '_cache';
    }

    public function getTtl(): int {
        return 3600;
    }

    public function findById(int $id): ?array {
        if (isset($this->cache[$id])) {
            return $this->cache[$id];
        }
        $user = ['id' => $id, 'name' => 'User ' . $id];
        $this->cache[$id] = $user;
        return $user;
    }

    public function findAdults(array $users): array {
        return array_filter($users, function($u) {
            return $u['age'] >= 18;
        });
    }
}

$service = new UserService();
$user = $service->findById(1);
echo $user['name'];

<?php and ?> tags are kept at column 0. PSR-12 standard requires 4-space indentation. For stricter PSR compliance, use PHP CS Fixer or Laravel Pint.

PHP Beautifier: Format PHP Code Without a Build Tool

PHP code from legacy projects, code generators, or quick experiments is often inconsistently indented. This beautifier applies brace-depth indentation to classes, interfaces, functions, and control structures — producing readable PHP that follows the PSR-12 indentation standard.

Formula
// Input: flat class UserService { private array $cache = []; public function findById(int $id): ?array { if (isset($this->cache[$id])) { return $this->cache[$id]; } return null; } } // Output: indented class UserService { private array $cache = []; public function findById(int $id): ?array { if (isset($this->cache[$id])) { return $this->cache[$id]; } return null; } }

PHP CS Fixer and PSR-12 specify 4-space indentation. <?php and ?> tags are always at column 0.

PSR-12 Indentation

PSR-12 (the current PHP coding standard) specifies 4-space indentation using spaces, not tabs. Opening braces for control structures (if, for, while, foreach, switch) go on the same line; opening braces for classes and methods go on the next line. This formatter applies 4-space indentation (the dominant standard) by default.

PHP Tags and Mixed Files

PHP files begin with to switch back to HTML. These tags are kept at column 0 since they are not part of the PHP block structure. Code inside the PHP block is indented normally using brace depth.

PHP CS Fixer for Production Code

PHP CS Fixer is the standard tool for PHP formatting in production projects. Run 'composer require --dev friendsofphp/php-cs-fixer' and configure it with a .php-cs-fixer.php file. Laravel Pint (composer require --dev laravel/pint) provides a zero-configuration wrapper for Laravel projects.

Practical Examples

Formatting a Legacy PHP Class

Clean up an old PHP file with inconsistent or no indentation.

  • 1.Paste the PHP class or function code
  • 2.Formatter applies 4-space indentation (PSR-12 standard)
  • 3.Review method bodies, control structures, and property declarations
  • 4.Copy the formatted output for review or IDE import

What Gets Formatted

  • class, interface, trait, function method blocks
  • if/else, for, foreach, while, switch blocks
  • tags kept at column 0
  • 2 or 4 space indentation toggle (PSR-12 recommends 4)

Good Use Cases

  • Cleaning up AI-generated PHP code
  • Reformatting legacy PHP from PHP 5 projects
  • Quick inspection before adding to a project
  • Understanding a third-party PHP library's code structure

Frequently Asked Questions

What PHP coding standard should I use?

PSR-12 is the current PHP coding standard, maintained by the PHP-FIG. It extends PSR-2 and specifies 4-space indentation, opening braces on the same line for control structures and the next line for classes/methods, and blank lines between method declarations.

Are <?php and ?> tags handled?

Yes. The closing tag are always output at column 0, regardless of the surrounding context. Code inside the PHP block is indented normally.

What is PHP CS Fixer?

PHP CS Fixer (php-cs-fixer) is the de facto PHP code formatter. It can enforce PSR-12, Symfony, and custom coding standards. Run 'php-cs-fixer fix src/' to format all PHP files in a directory. Laravel Pint is a zero-config wrapper around PHP CS Fixer.

Does this handle heredoc and nowdoc strings?

Heredoc (<<