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

Constants (define vs const)

php PHP 5.0+ Beginner

Also Known As

PHP constants define() const keyword PHP const

TL;DR

Named immutable values in PHP — const is compile-time and class-scoped; define() is runtime and supports dynamic names.

Explanation

PHP has two ways to define constants: const NAME = value (compile-time, usable in class bodies, interfaces, and at the top level, but not inside functions or conditionals) and define('NAME', value) (runtime, supports variable names, works anywhere). Class constants (const) support visibility modifiers since PHP 7.1 and typed constants since PHP 8.3. Use const for class-level constants and top-level application configuration; use define() when the constant name or value must be computed at runtime. Constants are globally accessible by name and cannot be redefined — prefer class constants or enums for namespaced, typed constant groups.

Common Misconception

define() and const are fully interchangeable for declaring constants. const is resolved at compile time and can be used in class bodies; define() is resolved at runtime, supports dynamic names, and works inside conditionals — const is preferred for class and namespace constants.

Why It Matters

Constants defined with const or define() cannot be changed at runtime — they communicate intent clearly and prevent accidental reassignment of values that should never vary.

Common Mistakes

  • Using define() inside functions or conditionally — prefer const at the top level for clarity and performance.
  • Class constants not declared final in PHP 8.1 — subclasses can override them, violating the 'constant' contract.
  • Using magic numbers instead of named constants — the meaning is lost and updates require searching the whole codebase.
  • Naming constants with camelCase instead of SCREAMING_SNAKE_CASE — breaks PHP convention and reduces readability.

Code Examples

✗ Vulnerable
// Magic number — meaning unknown:
if ($status === 3) { /* What is 3? */ }

// Named constant:
const ORDER_STATUS_SHIPPED = 3;
if ($status === ORDER_STATUS_SHIPPED) { /* Clear */ }

// Non-final class constant — overrideable:
class Config {
    const TIMEOUT = 30; // Should be: final const TIMEOUT = 30; (PHP 8.1+)
}
✓ Fixed
// define() — runtime, works anywhere
define('MAX_UPLOAD', 10 * 1024 * 1024);
define('APP_ENV', getenv('APP_ENV') ?: 'production');

// const — compile-time, preferred in classes/namespaces
class Order {
    const STATUS_PENDING   = 'pending';
    const STATUS_PAID      = 'paid';
    const STATUS_CANCELLED = 'cancelled';
}
echo Order::STATUS_PAID;

// PHP 8.3 — typed class constants
class Config {
    const string VERSION     = '2.4.1';
    const int    MAX_RETRIES = 3;
}

// Useful built-in constants:
// PHP_EOL, PHP_INT_MAX, PHP_INT_SIZE
// DIRECTORY_SEPARATOR, PATH_SEPARATOR

Tags


Added 15 Mar 2026
Edited 22 Mar 2026
Views 19
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 3 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 2 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 8 Perplexity 4 Unknown AI 3 Google 2 Ahrefs 1
crawler 16 crawler_json 1 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Use class constants (const STATUS_ACTIVE = 'active') scoped to the class they belong to; use PHP 8.1 enums instead of string constants for type safety; avoid global define() constants except for truly global config
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
define() for domain constants that should be class constants; repeated magic string that should be a constant; constant not used in same class it's defined
Auto-detectable: ✓ Yes phpcs phpstan rector
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✓ Auto-fixable Fix: Low Context: File

✓ schema.org compliant