In PHP, a constant is an identifier that is not changeable during script execution. Usually, we define constants through the define() or const keyword. But sometimes during development or debugging, we need to see which constants are defined in the current script or application. At this point, the PHP built-in function get_defined_constants() comes in handy.
This function can be used in two ways:
get_defined_constants() : Returns an associative array containing all defined constants.
get_defined_constants(true) : Returns a multi-dimensional array, grouping constants by their source, such as internal , user (user-defined), etc.
This article will focus on get_defined_constants(true)['user'] , that is, view all user-defined constants .
<?php
define('SITE_NAME', 'Sample website');
define('BASE_URL', 'https://m66.net');
$user_constants = get_defined_constants(true)['user'];
echo '<pre>';
print_r($user_constants);
echo '</pre>';
After running the above code, the output is similar to the following:
Array
(
[SITE_NAME] => Sample website
[BASE_URL] => https://m66.net
)
We can see that only constants that we explicitly declare through define() will appear in the user group. Constants defined by default within PHP (such as E_ALL , PHP_VERSION , etc.) will not be included.
In large projects, constant definitions may be distributed across multiple files. Use get_defined_constants(true)['user'] to quickly list all custom constants in the current environment, helping developers to confirm whether a constant has been repeatedly defined, overwritten, or misspelled.
In some configuration management systems, we may want to export all custom constants to JSON or save in other formats. With json_encode() , this function is easy to implement.
<?php
define('API_URL', 'https://m66.net/api');
define('VERSION', '1.0.0');
$user_constants = get_defined_constants(true)['user'];
header('Content-Type: application/json');
echo json_encode($user_constants, JSON_PRETTY_PRINT);
Output:
{
"API_URL": "https://m66.net/api",
"VERSION": "1.0.0"
}
In some custom plugins or frameworks, we may want the plugin to automatically recognize the constant configuration of the current environment when registering. Through this function, all defined constants can be obtained without intrusion and business logic processing or compatibility checks.
Only constants explicitly declared via define() or const will be classified as user .
If a third-party library is used, the constants defined at initialization will also be considered as user constants.
It is not recommended to frequently call this function in production environments to output the results, especially to expose its content to the user, as it may contain sensitive configuration information.
get_defined_constants(true)['user'] is a very practical debugging and configuration helper. It provides a simple and intuitive interface in scenarios where current custom constants need to be reviewed. Whether it is outputting logs, generating configuration snapshots, or performing compatibility checks, it is a powerful tool worth mastering.