In PHP, a constant is an identifier whose value cannot be changed during script execution. Usually, the define() function is used to define constants, or the const keyword can be used. However, in some scenarios, we may need to check whether a constant has been defined, especially if these constants may be loaded dynamically by third-party libraries, configuration files, or other modules.
In addition to the commonly used defined() function, PHP also provides a built-in function get_defined_constants() , which can return all defined constants and present them as arrays. By using this in conjunction with the array_key_exists() function, we can achieve more flexible and precise constant existence detection. This article will introduce usage and practical application examples of this combination.
The get_defined_constants() function returns a multidimensional array containing all defined constants. The structure of the array is as follows:
[
'Core' => [...],
'pcre' => [...],
'user' => [
'MY_CONSTANT' => 'value',
...
],
...
]
Among them, the user part is a constant defined by the user, while the other parts are defined by PHP built-in or extended.
By getting the get_defined_constants(true)['user'] array, we can list all user-defined constants and use array_key_exists() to determine whether a constant name exists in it. For example:
define('MY_SITE_URL', 'https://m66.net');
$userConstants = get_defined_constants(true)['user'];
if (array_key_exists('MY_SITE_URL', $userConstants)) {
echo 'constant MY_SITE_URL Defined,The value is:' . $userConstants['MY_SITE_URL'];
} else {
echo 'constant MY_SITE_URL Undefined';
}
In this example, we first define the constant MY_SITE_URL , and then use get_defined_constants(true)['user'] to obtain all user-defined constants and use array_key_exists() to make judgments.
Although using the defined() function can also check whether a constant exists, when we need to batch analyze or debug the definition of all constants in the current system, get_defined_constants() combined with array operations has an advantage. For example, you can print all constants starting with MY_ using the following code:
foreach (get_defined_constants(true)['user'] as $key => $value) {
if (strpos($key, 'MY_') === 0) {
echo "$key => $value\n";
}
}
The following is an example of configuration judgment, which is particularly common in multi-environment deployments:
// config.php
define('ENVIRONMENT', 'production');
// init.php
$userConstants = get_defined_constants(true)['user'];
if (array_key_exists('ENVIRONMENT', $userConstants)) {
if ($userConstants['ENVIRONMENT'] === 'production') {
error_reporting(0);
} else {
error_reporting(E_ALL);
}
} else {
// Default environment
define('ENVIRONMENT', 'development');
error_reporting(E_ALL);
}
In this way, we can not only ensure that the existence of constants is more flexible, but also prevent warnings caused by repeated definitions of constants.
Combining get_defined_constants() and array_key_exists() is a more powerful and more extensible way than the traditional defined() function. It allows us to deeply understand and manage constant definitions in the current script run environment, especially in large projects and multi-module architectures. Whether it is used for debugging, configuration checking, or runtime control logic, this method is worth mastering and utilizing.