PHP stops with Error: Undefined constant
Fatal error: Uncaught Error: Undefined constant "Constant"Short answer
PHP 8 throws an Error when code names a constant that does not exist. Before PHP 8 it treated the bare word as a string. So an unquoted array key, a constant spelled in the wrong case, or a define() that never ran will now stop the script. Quote the string, match the case, or define the constant first.
Fix it fast
1Find out which cause you have
Read the constant name in the message and the file and line next to it. Then ask PHP whether that name is defined at that point in the run.
<?php var_dump(defined('MY_NAME')); print_r(array_keys(get_defined_constants(true)['user'] ?? []));2Match what you see, then jump to its fix
- 1
The constant name looks like ordinary text, for example an array key such as $row[name] or an argument like mode.A string was written without quotesFix - 2
The message shows a name like Constant while the code defined CONSTANT.The constant is spelled with different letters in upper or lower caseFix - 3
defined('NAME') returns false at the line that fails, but the constant exists in some config file.The constant is defined later, or in a file that was not loadedFix - 4
The constant is defined in the code, yet the error appears on some requests only.A define() sits inside a branch that did not run, or const sits inside a function or if blockFix
- 1
What does the error look like?
Fatal error: Uncaught Error: Undefined constant "Constant"Use of undefined constant
Cause 1 of 4
A string was written without quotes
1Check
Look at the line from the error. A word with no quotes and no dollar sign is read as a constant. PHP 7 turned it into a string and only warned; PHP 8 throws.
grep -n -E '\[[a-z_]+\]' app/page.php2Fix
Put quotes around the word so it is a string.
// before
echo $row[name];
// after
echo $row['name'];3Verify
Reload the page. The Error is gone and the value prints.
php -l app/page.php && php app/page.phpConfirmed by php.net
Cause 2 of 4
The constant is spelled with different letters in upper or lower case
1Check
Constant names are case-sensitive. Compare the name in the message with the name passed to define() or const.
grep -rn "define('\|^const " src/ config/2Fix
Use the exact spelling everywhere, or search the project and rename the odd one out.
define("CONSTANT", "Hello world.");
echo CONSTANT; // works
// echo Constant; // Error: Undefined constant "Constant"3Verify
Run the file again.
php app/page.phpConfirmed by php.net
Cause 3 of 4
The constant is defined later, or in a file that was not loaded
1Check
A constant created with define() only exists after that line has run. One created with const is set at compile time, but only if its file was included. Check the order of require statements.
php -r "require 'config.php'; var_dump(defined('DB_HOST'));"2Fix
Load the file that defines the constant before the code that reads it. If the name is only known at run time, test it first and read it with constant().
require __DIR__ . '/config.php';
$name = 'APP_ENV';
if (defined($name)) {
echo constant($name);
}3Verify
Run the script. The failing line now sees the value.
php index.phpConfirmed by php.net
Cause 4 of 4
A define() sits inside a branch that did not run, or const sits inside a function or if block
1Check
The manual says const must be declared at the top level, not inside functions, loops, if statements or try/catch blocks. define() may run conditionally, so it will be skipped when the condition is false.
grep -n -B3 "define(" config.php2Fix
Move const to the top level, or give define() a path that always runs (for example an else branch with a default).
if (!defined('APP_ENV')) {
define('APP_ENV', 'production');
}3Verify
Request the page on the path that failed before.
php -r "require 'config.php'; echo APP_ENV;"Confirmed by php.net
How do I stop it coming back?
- Always quote array keys and string arguments; run php -l on every changed file.
- Keep all constants in one config file that is required first, and use one naming style (UPPER_SNAKE_CASE).
- Use defined() before reading a constant that may not exist.
- When upgrading from PHP 7, run the code with error_reporting(E_ALL) first, because the old warning was the hint.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix