ThakurCoder
PHP Coding Style Standards
Complete reference guide for PHP coding style standards with detailed code examples
PSR-1: Basic Coding Standard
PSR-2: Coding Style Guide
PSR-12: Extended Coding Style Guide
📐

PSR-1: Basic Coding Standard

Accepted

Establishes the basic coding standard that all PHP code must follow.

Code Examples

PHP Tags
Files MUST use only <?php and <?= tags.
Non-compliant
<?php
// Bad: Using short tags
<?
echo "Hello";
?>
PSR-compliant
<?php
// Good: Using only <?php tag
echo "Hello";
// Note: Closing ?> tag should be omitted in files containing only PHP
Character Encoding
Files MUST use UTF-8 encoding without BOM.
<?php
// File must be saved as UTF-8 without BOM
// This ensures proper character handling across different systems
Side Effects
Files SHOULD either declare symbols (classes, functions, constants) OR cause side effects, but not both.
Non-compliant
<?php
// Bad: Mixing declarations and side effects
namespace VendorPackage;

class MyClass
{
    // ...
}

// Side effect: output
echo "This file was loaded";
ini_set('error_reporting', E_ALL);
PSR-compliant
<?php
// Good: Only declarations
namespace VendorPackage;

class MyClass
{
    // ...
}

// Separate file for side effects:
// <?php
// require 'vendor/autoload.php';
// echo "Application started";
Class Naming
Classes MUST be declared in StudlyCaps.
Non-compliant
<?php
class myclass {
    // ...
}

class my_class {
    // ...
}
PSR-compliant
<?php
class MyClass {
    // ...
}

class MyOtherClass {
    // ...
}
Method Naming
Method names MUST be declared in camelCase.
Non-compliant
<?php
class MyClass {
    public function GetName() {
        // ...
    }
    
    public function get_name() {
        // ...
    }
}
PSR-compliant
<?php
class MyClass {
    public function getName() {
        // ...
    }
    
    public function getFullName() {
        // ...
    }
}
Constants
Class constants MUST be declared in all upper case with underscore separators.
Non-compliant
<?php
class MyClass {
    const version = '1.0';
    const dateApproved = '2012-06-15';
}
PSR-compliant
<?php
class MyClass {
    const VERSION = '1.0';
    const DATE_APPROVED = '2012-06-15';
    const MAX_ITEMS = 100;
}
Namespace and Class Names
Namespaces and classes MUST follow an autoloading standard (PSR-0 or PSR-4).
<?php
// PSR-4 compliant structure
namespace VendorPackage;

use VendorPackageSubNamespaceClassA;
use VendorPackageSubNamespaceClassB;

class MyClass
{
    // ...
}

About PHP Coding Style Standards

PHP coding style standards ensure consistency and readability across PHP projects. PSR-1 establishes the basic coding standard that all PHP code must follow. PSR-2 extends PSR-1 with additional style rules but has been deprecated in favor of PSR-12, which is the current recommended extended coding style guide. These standards help reduce cognitive friction when reading code from different authors and make collaboration easier.

3 Coding Style Standards
Source: PHP-FIG
Recommended: PSR-12