Current Location: Home> Latest Articles> The combined use of defined() and constant() function

The combined use of defined() and constant() function

gitbox 2025-05-28

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.

1. Basic concepts

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

2. The conventional way to define constants

Use define() to define constants:

 define('APP_ENV', 'production');

Its value can then be directly referenced through the APP_ENV constant.

3. Use defined() to check whether the constant exists

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';
}

4. Use constant() to dynamically obtain the value of a constant

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

5. Examples of joint use scenarios

1. Configuration loading

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

2. Dynamic permission control

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';
}

6. Things to note

  1. The constant name passed in constant() must be a string. If the constant does not exist, Warning will be thrown.

  2. Using defined() can safely prevent errors caused by undefined constants.

  3. 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).

7. Summary

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.