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
Check the constant name
Verify the constant name for typos. Constants are case-sensitive by default.
- 2
Ensure it is defined
Check that define('APP_ENV', 'value') or const APP_ENV = 'value' is called before the constant is used.
- 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.
Got the actual stack trace?
Paste it into our free AI explainer to get the cause and the fix for your specific case — no signup, nothing to install.
Explain my errorBugsly 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