Skip to content

PHP Tutorial Modern PHP Features

Modern PHP Coding Style

Lesson 237 of 369 3 min read By Deepak Paswan Updated Oct 5, 2026

Style rules sound dull, but they decide how easily your code can be read, shared and reviewed. PHP has a widely accepted standard: PSR-12, from the PHP-FIG group.

The main rules

  1. Files use <?php and no closing tag (pure PHP files).
  2. Use 4 spaces to indent, not tabs.
  3. One class per file, and the file name equals the class name.
  4. Class names in PascalCase, methods and variables in camelCase, constants in UPPER_SNAKE_CASE.
  5. Opening braces: on a new line for classes and methods, on the same line for if, loops and closures.
  6. A blank line after the namespace and after the use block.
  7. Keep lines reasonably short (about 80 to 120 characters).
  8. Add declare(strict_types=1); after the opening tag.

A well-formatted file

index.php
PHP
<?php

declare(strict_types=1);

namespace App\Service;

use InvalidArgumentException;

final class PriceCalculator
{
    private const TAX_RATE = 0.18;

    public function __construct(private readonly string $currency = "INR")
    {
    }

    public function withTax(float $price): float
    {
        if ($price < 0) {
            throw new InvalidArgumentException("Price cannot be negative.");
        }

        return round($price * (1 + self::TAX_RATE), 2);
    }
}

echo (new PriceCalculator())->withTax(100);
Output
OUTPUT
118

Naming

ThingStyleExample
Class, interface, trait, enumPascalCaseUserRepository
Method, functioncamelCasefindByEmail()
Variable, propertycamelCase$orderTotal
ConstantUPPER_SNAKEMAX_RETRIES
NamespacePascalCaseApp\Http\Controllers

Name things for what they are, not how they work. $users is better than $arr, and calculateTotal() is better than doStuff().

Booleans and methods

Start boolean-returning methods with is, has, can or should: isActive(), hasPermission().

Comments and docblocks

  • Write a comment to explain why, not what.
  • If a type is clear from the code, you do not need a docblock. Use docblocks for things the type system cannot say, such as @param string[] $tags or @throws.

Tools that enforce style

You do not need to remember the rules. Tools check and fix them.

ToolWhat it does
PHP_CodeSniffer (phpcs, phpcbf)Checks and fixes code against a standard such as PSR-12
PHP CS FixerRewrites code to match a style
Laravel PintA ready-made fixer based on PHP CS Fixer
PHPStan / PsalmStatic analysis: finds type and logic mistakes
Editor formattingVS Code and PhpStorm can format on save

Install them with Composer (see What Is Composer?):

terminal
BASH
composer require --dev squizlabs/php_codesniffer
vendor/bin/phpcs --standard=PSR12 src/

Consistency beats perfection

If a project already has a style, follow it, even if you would choose differently. A file that matches the rest of the project is easier to read than one that is “better” but different.

The WordPress exception

WordPress has its own coding standards (tabs, snake_case function names, spaces inside brackets). When you write code for a WordPress theme or plugin, follow the WordPress standard. For everything else, use PSR-12.

lightbulb
Automate it
Set up a formatter to run on save, and a style check in your build. Then nobody argues about spaces in code reviews.