In PHP, constants are identifiers that cannot be changed during script execution and are usually used to store data that does not change globally. Defining constants usually uses the define() function. When determining whether a constant exists and dynamically accesses its value at runtime, the two functions defined() and constant() are commonly used. This article will introduce in-depth how to use these two functions and how to combine them to flexibly operate constants.
defined(string $name): bool
Determines whether a constant named $name has been defined. Returns a boolean value.
constant(string $name): mixed
Returns the value of the constant $name . If the constant is undefined, an error will be thrown.
These two functions are powerful tools for handling dynamic constant names, especially in scenarios such as configuration, plug-in loading, permission checking, etc.
Use define() to define constants:
define('APP_ENV', 'production');
Its value can then be directly referenced through the APP_ENV constant.
In some cases, constants may come from configuration files or external systems, we can check if they are defined before using:
if (defined('APP_ENV')) {
echo 'The current environment is: ' . APP_ENV;
} else {
echo 'Undefined environment constants';
}
If you want to reference constant names through variables, you need to use constant() :
$constName = 'APP_ENV';
if (defined($constName)) {
echo 'The current environment is: ' . constant($constName);
}
This method is often used in component configuration or plug-in mechanisms, such as:
$plugin = 'Uploader';
$constName = 'PLUGIN_' . strtoupper($plugin) . '_ENABLED';
if (defined($constName) && constant($constName) === true) {
echo "Plugin {$plugin} Enabled";
}
Suppose there is a multi-environment configuration file that dynamically loads the configuration according to the defined environment constants:
define('APP_ENV', 'development');
$configName = 'CONFIG_' . strtoupper(APP_ENV) . '_DB';
define('CONFIG_DEVELOPMENT_DB', 'mysql://user:[email protected]/devdb');
define('CONFIG_PRODUCTION_DB', 'mysql://user:[email protected]/proddb');
if (defined($configName)) {
$dbConfig = constant($configName);
echo "Database connection information:$dbConfig";
}
Suppose you define a series of permission constants:
define('PERM_USER_EDIT', true);
define('PERM_USER_DELETE', false);
You can use a function to combine defined() and constant() to determine permissions:
function hasPermission(string $perm): bool {
$permConst = 'PERM_' . strtoupper($perm);
return defined($permConst) && constant($permConst) === true;
}
if (hasPermission('user_edit')) {
echo 'You have permission to edit users';
}
The constant name passed in constant() must be a string. If the constant does not exist, Warning will be thrown.
Using defined() can safely prevent errors caused by undefined constants.
Constant names are case sensitive (unless the third parameter of define() is set to true , but this parameter has been deprecated after PHP 7.3).
defined() and constant() are powerful tools for handling dynamic constants, especially suitable for dynamic configuration loading, plug-in systems, permission control and other scenarios. Using these two functions reasonably can make your PHP program more flexible, robust and modular.