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?
A dynamic constant is not created by processing a literal expression but by invoking a so-called bootstrap method that produces the constant value as its result.
A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire script.
If you have defined a constant, it can never be changed or undefined. To define a constant you have to use define() function and to retrieve the value of a constant, you have to simply specifying its name. Unlike with variables, you do not need to have a constant with a $.
A variable of a variable takes a value of a variable and threads which is the name of a variable. This is new feature of using variables and by using double dollar signs. This technique is called a dynamic variable in PHP. Those variables you can use a dynamically generated variable of variable as well as OOP Concept.
http://dk.php.net/manual/en/function.constant.php
echo constant($constant_name);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With