{
    "slug": "php_constants",
    "term": "Constants (define vs const)",
    "category": "php",
    "difficulty": "beginner",
    "short": "Named immutable values in PHP — const is compile-time and class-scoped; define() is runtime and supports dynamic names.",
    "long": "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.",
    "aliases": [
        "PHP constants",
        "define()",
        "const keyword",
        "PHP const"
    ],
    "tags": [
        "php",
        "syntax"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "magic_number",
        "magic_strings",
        "enums",
        "magic_constants"
    ],
    "prerequisites": [
        "php_data_types",
        "magic_constants",
        "enums"
    ],
    "refs": [
        "https://www.php.net/manual/en/language.constants.php"
    ],
    "bad_code": "// Magic number — meaning unknown:\nif ($status === 3) { /* What is 3? */ }\n\n// Named constant:\nconst ORDER_STATUS_SHIPPED = 3;\nif ($status === ORDER_STATUS_SHIPPED) { /* Clear */ }\n\n// Non-final class constant — overrideable:\nclass Config {\n    const TIMEOUT = 30; // Should be: final const TIMEOUT = 30; (PHP 8.1+)\n}",
    "good_code": "// define() — runtime, works anywhere\ndefine('MAX_UPLOAD', 10 * 1024 * 1024);\ndefine('APP_ENV', getenv('APP_ENV') ?: 'production');\n\n// const — compile-time, preferred in classes/namespaces\nclass Order {\n    const STATUS_PENDING   = 'pending';\n    const STATUS_PAID      = 'paid';\n    const STATUS_CANCELLED = 'cancelled';\n}\necho Order::STATUS_PAID;\n\n// PHP 8.3 — typed class constants\nclass Config {\n    const string VERSION     = '2.4.1';\n    const int    MAX_RETRIES = 3;\n}\n\n// Useful built-in constants:\n// PHP_EOL, PHP_INT_MAX, PHP_INT_SIZE\n// DIRECTORY_SEPARATOR, PATH_SEPARATOR",
    "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",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_constants",
        "html_url": "https://codeclaritylab.com/glossary/php_constants",
        "json_url": "https://codeclaritylab.com/glossary/php_constants.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Constants (define vs const)](https://codeclaritylab.com/glossary/php_constants) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_constants"
            }
        }
    }
}