Current Location: Home> Latest Articles> Quickly locate custom constants using get_defined_constants(true)['user']

Quickly locate custom constants using get_defined_constants(true)['user']

M66 2025-05-26

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 .

Example: Get 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.

Purpose scenarios

1. Debug user constants

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.

2. Build a constant export tool

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"
}

3. Integrate with frameworks or plug-ins

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.

Things to note

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

summary

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.