{
    "slug": "error_reporting",
    "term": "Error Reporting & Display",
    "category": "php",
    "difficulty": "beginner",
    "short": "PHP's error_reporting level and display_errors directive control which errors are shown and where — development needs E_ALL with display on; production needs E_ALL with display off and logging on.",
    "long": "PHP has two distinct error controls: error_reporting sets which error types PHP generates (E_ALL includes all errors, warnings, notices, and deprecations); display_errors controls whether errors are output to the browser or CLI. The correct configuration differs by environment: Development — error_reporting(E_ALL), display_errors=On, display_startup_errors=On (see all errors immediately). Production — error_reporting(E_ALL), display_errors=Off, log_errors=On (log everything, show nothing to users). The E_ALL level includes E_NOTICE (undefined variables, array access), E_DEPRECATED (functions removed in future PHP versions), E_STRICT (code that may not be forward compatible), and E_WARNING. Setting error_reporting to E_ALL in development and fixing all notices prevents surprises when PHP version upgrades promote notices to errors. The @ error suppression operator silences individual expressions — a code smell that hides problems rather than fixing them.",
    "aliases": [
        "error_reporting",
        "display_errors",
        "E_ALL",
        "E_NOTICE",
        "E_WARNING",
        "error suppression",
        "@ operator"
    ],
    "tags": [
        "php",
        "errors",
        "debugging",
        "configuration",
        "production",
        "development",
        "php-ini"
    ],
    "misconception": "Setting error_reporting to E_ALL in development will break working code with unnecessary noise. E_NOTICE and E_DEPRECATED are not noise — they are warnings about real problems. An undefined variable notice means your code is reading from a variable that may not always be set; that is a real bug waiting for the right conditions. E_DEPRECATED warnings about removed functions are advance warning of breaking changes on the next PHP upgrade. Fixing all E_ALL warnings in development prevents production surprises.",
    "why_it_matters": "The single most common PHP debugging mistake is working with display_errors=Off in development or error_reporting set too low — errors happen silently, the developer adds debugging code hunting symptoms rather than seeing the cause immediately. Conversely, display_errors=On in production is a security issue that exposes file paths, database connection strings, and stack traces. Getting these two settings correct for each environment takes two minutes and saves hours of debugging time.",
    "common_mistakes": [
        "Using error_reporting(0) or @ suppression to hide errors instead of fixing them — suppressed errors still occur and cause incorrect behaviour.",
        "Setting display_errors=Off in development — makes debugging significantly harder; always show errors in development.",
        "Not setting display_errors=Off in production — raw PHP errors expose sensitive information to users and attackers.",
        "Checking for E_ALL without including custom error handlers — set_error_handler() captures errors that PHP would normally display; use it to log and handle errors programmatically."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "error_logging",
        "debugging",
        "php_ini"
    ],
    "prerequisites": [],
    "refs": [
        "https://www.php.net/manual/en/function.error-reporting.php"
    ],
    "bad_code": "// Suppressing instead of fixing\n$result = @file_get_contents($url); // hides errors\nif (!$result) { /* why did this fail? no idea */ }\n\n// Wrong production config\ndisplay_errors = On  ; php.ini — shows errors to users\nerror_reporting = E_ERROR ; misses warnings and notices",
    "good_code": "// Development: show everything\nif (getenv('APP_ENV') === 'development') {\n    error_reporting(E_ALL);\n    ini_set('display_errors', '1');\n    ini_set('display_startup_errors', '1');\n}\n\n// Production: log everything, show nothing\nif (getenv('APP_ENV') === 'production') {\n    error_reporting(E_ALL);\n    ini_set('display_errors', '0');\n    ini_set('log_errors', '1');\n    ini_set('error_log', '/var/log/php/app-errors.log');\n}\n\n// Fix instead of suppress\ntry {\n    $result = file_get_contents($url);\n    if ($result === false) throw new RuntimeException('Failed to fetch: ' . $url);\n} catch (RuntimeException $e) {\n    $this->logger->error($e->getMessage());\n}",
    "quick_fix": "Development: ini_set('display_errors', '1'); error_reporting(E_ALL); — Production: ini_set('display_errors', '0'); ini_set('log_errors', '1'); error_reporting(E_ALL);",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-23",
    "updated": "2026-04-04",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/error_reporting",
        "html_url": "https://codeclaritylab.com/glossary/error_reporting",
        "json_url": "https://codeclaritylab.com/glossary/error_reporting.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": "[Error Reporting & Display](https://codeclaritylab.com/glossary/error_reporting) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/error_reporting"
            }
        }
    }
}