Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP stops with Error: Undefined constant

ErrorFatal 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.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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'] ?? []));
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The message shows a name like Constant while the code defined CONSTANT.The constant is spelled with different letters in upper or lower caseFix
    3. 3defined('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. 4The 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

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.php

2Fix

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.php

Confirmed by php.net

Did this fix it?

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.php

Confirmed by php.net

Did this fix it?

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.php

Confirmed by php.net

Did this fix it?

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.php

2Fix

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

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.