Current Location: Home> Latest Articles> get_defined_constants() Why does it return nested arrays?

get_defined_constants() Why does it return nested arrays?

M66 2025-05-20

During PHP development, get_defined_constants() is a very useful function that returns all defined constants. However, when many developers use it the first time they are confused by its return results: Why does this function return a nested array instead of a simple list of key-value pairs?

Function introduction

get_defined_constants() is a built-in function that returns all defined constants in the current script. The syntax is as follows:

 array get_defined_constants ([ bool $categorize = false ] )

When we call get_defined_constants() and pass in the parameter $categorize = true , it returns the nested array by "categorization" of the constant. Even if the parameter is not explicitly passed in, some environments may return nested structures by default.

Why return nested arrays?

There are many types of constants in PHP, covering core language, extensions, user customization and other aspects. To display this information more clearly, PHP provides classification ways to organize constants. When you use:

 print_r(get_defined_constants(true));

You will see an output structure similar to the following:

 Array
(
    [Core] => Array
        (
            [E_ERROR] => 1
            [E_WARNING] => 2
            ...
        )

    [date] => Array
        (
            [DATE_ATOM] => Y-m-d\TH:i:sP
            ...
        )

    [user] => Array
        (
            [MY_CUSTOM_CONST] => 123
        )
)

This nested array structure makes it easier for us to understand which constants are defined by the core module, which are provided by extensions, and which are user-defined. This organization is useful when debugging and troubleshooting issues, especially in large projects or environments where multiple extensions are used.

What if you just want to get a pure list?

If you just want to get a simple list of constants without caring about the classification, you can set the parameter to false (or omit the parameter):

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

This will return a flat array, for example:

 Array
(
    [E_ERROR] => 1
    [E_WARNING] => 2
    [DATE_ATOM] => Y-m-d\TH:i:sP
    [MY_CUSTOM_CONST] => 123
)

This approach is suitable for situations where you need to quickly find a constant value, or if you don't care about where it comes from when processing a constant.

Practical application examples

When debugging, you may want to check if the custom constants are correctly defined:

 define('SITE_URL', 'https://m66.net');
$constants = get_defined_constants(true);

if (isset($constants['user']['SITE_URL'])) {
    echo "constant SITE_URL Defined,The value is:" . $constants['user']['SITE_URL'];
}

In this way, we can clearly locate the definition source of a certain constant to avoid the problem of constant naming conflicts in the project.

Conclusion

The reason why get_defined_constants() in PHP returns nested arrays is to enhance readability and maintainability, especially in large systems that can clearly distinguish the sources of constants. Although it looks complicated at first glance, once it understands its structure and purpose, it will become a powerful tool in the hands of developers.