Current Location: Home> Latest Articles> Why do some constants appear as null values?

Why do some constants appear as null values?

M66 2025-05-22

The get_defined_constants function returns an array containing all defined constants and their corresponding values. Const is defined by the define() or const keyword, and they are unchangeable throughout the script.

For example, the following code demonstrates how to define constants and use get_defined_constants to view defined constants:

 define("SITE_URL", "https://m66.net");
define("SITE_NAME", "M66");

$constants = get_defined_constants();
print_r($constants);

The output will be similar to:

 Array
(
    [SITE_URL] => https://m66.net
    [SITE_NAME] => M66
    ...
)

This way, you can see all the defined constants and their values.

Why do some constants appear as null values?

Sometimes, when getting_defined_constants is executed, the values ​​of some constants will appear empty. This situation may be caused by several reasons:

  1. Constants are not assigned correctly <br> A constant must be assigned a value immediately when defined. If a constant is not properly provided in the define() or const statement, or the value is an empty string, the value of this constant will be displayed as empty when obtained through get_defined_constants .

     define("EMPTY_CONSTANT", "");
    

    The above code defines a constant EMPTY_CONSTANT with an empty string. When get_defined_constants is called, the output will display:

     [EMPTY_CONSTANT] => 
    
  2. Scope of constant definition <br> In PHP, the scope of constants is usually global. But if you define constants within certain scopes (such as inside functions), they may not be returned by get_defined_constants .

     function defineInsideFunction() {
        define("FUNC_CONST", "Hello, World!");
    }
    
    defineInsideFunction();
    print_r(get_defined_constants());
    

    If you define a constant inside the function, it may not appear in the result of get_defined_constants . To avoid this problem, make sure that the definition of constants is performed in the global scope.

  3. Conditional constant definition <br> If the definition of a constant is wrapped in some conditional statements and the condition is not satisfied, the constant will not be defined. Therefore, these constants may not be displayed when fetched via get_defined_constants .

     if (false) {
        define("CONDITION_CONST", "This will not be defined");
    }
    

    In this case, CONDITION_CONST will not appear in the result of get_defined_constants , because the conditional judgment fails and the constant is not defined.

  4. Missing predefined constants
    PHP itself defines some predefined constants at runtime, such as PHP_VERSION , PHP_OS , etc. But in some special cases, such as custom environment configurations, PHP may not load predefined constants, so they may not be displayed.

    In some specific environments, certain constants may vary depending on the environment. If you see predefined constants empty in a specific configuration, you may need to check your PHP configuration or running environment.

How to avoid the situation where constants are null?

To avoid constants showing null values, make sure you are defining constants:

  1. Make sure that the constant has a valid value when defined and is not an empty string.

  2. Ensure that the definition of constants is performed globally and avoid being defined only within local scopes.

  3. When defining a constant in a conditional statement, it is necessary to ensure that the condition always holds, or to make conditional judgments before the constant definition.

  4. If the predefined constant is missing, check your PHP environment configuration to ensure that the relevant constants are loaded correctly at runtime.