Dynamic constant name in PHP
I am trying to create a constant name dynamically and then get at the value.
define( CONSTANT_1 , "Some value" ) ;
// try to use it dynamically ...
$constant_number = 1 ;
$constant_name = ("CONSTANT_" . $constant_number) ;
// try to assign the constant value to a variable...
$constant_value = $constant_name;
But I find that $constant value still contains the NAME of the constant, and not the VALUE.
I tried the second level of indirection as well $$constant_name
But that would make it a variable not a constant.
Can somebody throw some light on this?
http://dk.php.net/manual/en/function.constant.php
echo constant($constant_name);
And to demonstrate that this works with class constants too:
class Joshua {
const SAY_HELLO = "Hello, World";
}
$command = "HELLO";
echo constant("Joshua::SAY_$command");