How can constants be accessed as variables in PHP?

Constants in PHP can be accessed as variables by using the constant() function. This function takes the constant name as a string parameter and returns its value. This allows constants to be used in places where only variables are expected, such as in function arguments or array keys.

define('MY_CONSTANT', 'Hello World');

// Access constant as variable
$constantValue = constant('MY_CONSTANT');
echo $constantValue; // Output: Hello World