Undefined Constant

Fatal error: Uncaught Error: Undefined constant "APP_ENV"

Quick Answer

You are using a constant that has not been defined. Check that the constant is defined with define() or const before it is used, or that the file defining it is loaded.

Why This Happens

Prior to PHP 8.0, using an undefined constant was treated as a string and raised a notice. In PHP 8.0+, it throws a fatal Error. This commonly happens when a config file that defines constants is not loaded, the constant name is misspelled, or you forgot to use quotes around a string.

The Problem

// config.php was not loaded
if (APP_ENV === 'production') {
    // ...
}

The Fix

require_once __DIR__ . '/config.php'; // Defines APP_ENV
if (APP_ENV === 'production') {
    // ...
}

// Or use a default:
$env = defined('APP_ENV') ? APP_ENV : 'development';

Step-by-Step Fix

  1. 1

    Check the constant name

    Verify the constant name for typos. Constants are case-sensitive by default.

  2. 2

    Ensure it is defined

    Check that define('APP_ENV', 'value') or const APP_ENV = 'value' is called before the constant is used.

  3. 3

    Load the config file

    If the constant is defined in a separate file, ensure that file is loaded before the code that uses the constant.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free