← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

PSR-1: Basic Coding Standard

style PHP 5.3+ Beginner

Also Known As

PSR-1 PSR-1 basic coding standard

TL;DR

The foundational PHP-FIG standard defining file encoding, PHP tags, namespace/class naming, and the single-responsibility rule for files.

Explanation

PSR-1 establishes baseline rules all PHP code should follow: files must use only <?php or <?= tags, use UTF-8 without BOM, and either declare symbols (classes, functions, constants) or cause side effects (output, config changes) — but not both in the same file. Class names must be in PascalCase, class constants in UPPER_SNAKE_CASE, and method names in camelCase. These rules underpin PSR-4 autoloading and PSR-12 style and are enforced by PHP_CodeSniffer. PSR-1 compliance is a prerequisite for all PHP-FIG standards.

Common Misconception

PSR-1 is an old standard superseded by PSR-12 and no longer relevant. PSR-1 defines the foundational rules (PHP tags, file encoding, class/method naming) that PSR-12 builds upon — PSR-12 extends PSR-1 rather than replacing it, so PSR-1 compliance is always required.

Why It Matters

PSR-1 defines PHP's basic coding standard — files must use only <?php or <?= tags, use UTF-8 without BOM, and either declare symbols or cause side effects, not both in the same file.

Common Mistakes

  • Mixing class definitions and side effects (echo, function calls) in the same file.
  • Using short open tags <? instead of <?php — not all servers have short_open_tag enabled.
  • Files with BOM (byte order mark) before <?php — causes 'headers already sent' errors.
  • StudlyCaps class names vs camelCase method names — PSR-1 requires both.

Code Examples

✗ Vulnerable
<?php
// PSR-1 violation — side effect (echo) and symbol declaration in same file:
echo 'Loading...'; // Side effect

class UserRepository { // Symbol declaration
    public function find(int $id): User {}
}
// Fix: move the echo to the caller, keep this file declaration-only
✓ Fixed
<?php
// PSR-1 Basic Coding Standard — key rules:

// 1. Files MUST use only <?php or <?= tags (not short <? tags)
// 2. PHP files MUST use only UTF-8 without BOM
// 3. Files SHOULD either declare symbols OR cause side effects — not both

// Side effects: echo, require, config changes
// Declarations: class, function, const

// Bad: declarations + side effects in same file
<?php
echo 'Loading...'; // side effect
class MyClass {}    // declaration — these shouldn't mix

// Good: separate files
// src/MyClass.php       — declarations only
// public/bootstrap.php  — side effects only

// 4. Namespaces and classes MUST follow an autoloading PSR (PSR-4)
// 5. Class names MUST be in StudlyCaps
// 6. Method names MUST be in camelCase

Added 15 Mar 2026
Edited 22 Mar 2026
Views 25
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 3 pings T 0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 2 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T
No pings yesterday
Amazonbot 9 Perplexity 5 Google 2 Unknown AI 2 SEMrush 2 Ahrefs 1
crawler 20 crawler_json 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
PSR-1 is the foundation: PHP files must use only <?php or <?= tags, must use UTF-8 without BOM, must declare symbols OR cause side-effects but not both
📦 Applies To
PHP 5.3+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
PHP file with both class declaration AND echo/output statements; short tag <? without php; file with BOM byte order mark; underscore-prefixed class names
Auto-detectable: ✓ Yes phpcs php-cs-fixer
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: File

✓ schema.org compliant