PHP Reference ยท Concepts and Reference
PHP coding standards
A quick reference to PHP naming and formatting conventions: classes, methods, constants, indentation, braces, imports, types and tools that enforce them.
A coding standard is a set of rules for how PHP source is named and laid out. PHP does not enforce one, but nearly every modern project follows PSR-12, so readers can skim any codebase without relearning the layout. This page is a compact cheat sheet of the conventions, followed by a small checker you can run.
Consistency matters more than any single rule. Pick the standard, automate it with a formatter, and stop debating braces in code review.
<?php declare(strict_types=1); namespace App\Billing; use InvalidArgumentException; final class InvoiceTotal { public const TAX_RATE = 0.2; public function gross(float $net): float { return $net * (1 + self::TAX_RATE); } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Naming
| Element | Convention | Example |
|---|---|---|
| Class, interface, trait, enum | StudlyCaps |
OrderRepository |
| Enum case | StudlyCaps (common) |
Status::InProgress |
| Method, function | camelCase (functions in older code: snake_case) |
findByEmail() |
| Property, variable, parameter | camelCase (or consistently snake_case) |
$firstName |
| Class constant | UPPER_SNAKE_CASE |
MAX_RETRIES |
| Namespace | StudlyCaps segments, vendor first |
Acme\Billing |
| File | One class per file, named like the class | OrderRepository.php |
Formatting (PSR-12 highlights)
- Files use
<?phponly (no closing?>in pure-PHP files), UTF-8 without BOM and Unix line endings. - Four spaces per indent level, never tabs. Soft line limit of 120 characters.
- One blank line after
namespace, and one after the block ofuseimports. Imports are ordered and one per line. - Opening brace of a class or method goes on its own line. Braces of
if,foreachandwhilestay on the same line. - Always use braces for control structures, and write
elseifas one word. - One statement per line. Visibility (
public,private) on every property and method;abstractandfinalcome before it,staticafter. - Declare
strict_types=1right after the opening tag in application code.
Usage notes
- Do not mix standards in one codebase. Laravel Pint, PHP CS Fixer and PHP_CodeSniffer rewrite files to a preset in seconds.
- Type every parameter, property and return when you can. It is documentation that PHP enforces.
- Prefer descriptive names (
isRegisteredForDiscounts) over comments that explain a vague one. - Put the configuration (
pint.json,.php-cs-fixer.php,phpcs.xml) in the repository so everyone formats alike, and run the tool in CI.