In PHP, get_defined_constants() is a very practical function that can be used to get all defined constants in the current script. This function is especially useful when we are debugging or want to understand the state of constants in the running environment.
This function can accept a Boolean parameter: category . When you call get_defined_constants(true) and get_defined_constants(false) , the return result is different. Understanding their differences will help us better use this function for debugging or analysis.
When you call get_defined_constants(false) (or call directly without passing parameters), the return is a one-dimensional array, the key name is the name of the constant, and the key value is the corresponding value. For example:
print_r(get_defined_constants(false));
Possible output:
Array
(
[E_ERROR] => 1
[E_WARNING] => 2
[E_PARSE] => 4
...
)
This is all currently defined constants, not grouped, and output directly with the constant name as the key name. This pattern is more intuitive and concise, and is suitable for quick viewing of which constants exist and their values.
When you call get_defined_constants(true) , the returned two-dimensional array is, that is, these constants will be classified and grouped by "module". Common groups include Core , pcre , date , user , etc. This pattern is more suitable for scenarios where constants are required to be managed in a classified manner.
For example:
print_r(get_defined_constants(true));
The output may be similar:
Array
(
[Core] => Array
(
[E_ERROR] => 1
[E_WARNING] => 2
...
)
[pcre] => Array
(
[PREG_PATTERN_ORDER] => 1
...
)
[user] => Array
(
[MY_CONSTANT] => 123
)
)
This structure allows for a clearer understanding of the source of constants, which are helpful for analyzing which are defined by PHP core, which are provided by extensions, and which are user-defined.
How to use | Return to the structure | Suitable for use |
---|---|---|
get_defined_constants(false) | One-dimensional array | Simple debugging, quick traversal |
get_defined_constants(true) | Two-dimensional array (category) | In-depth analysis to distinguish sources |
If you just want to get user-defined constants (non-PHP core or extension definitions), using get_defined_constants(true)['user'] is a very concise way:
define('SITE_NAME', 'MySite');
define('SITE_URL', 'https://m66.net');
$userConstants = get_defined_constants(true)['user'];
print_r($userConstants);
Output result:
Array
(
[SITE_NAME] => MySite
[SITE_URL] => https://m66.net
)
This is especially useful in large projects and can help you quickly check if all expected custom constants are defined.
get_defined_constants() is one of the very valuable tool functions in PHP. When you need a tile list of constants, use false or not to pass parameters; when you need a more detailed classification of constant sources, using true is a better choice. Mastering the difference between these two usages allows you to use constant information more efficiently in development and debugging.