• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

PHP - Constants and variables

jjones

Lifer
In PHP you can create variable variables. Is there any way to do something similar with constants?

I'm using some variable variables like this:

$a = 'cat';

$b = 'a';

$c = $$b; (which is the same as $c = 'cat'😉

I want to do the same type of thing with a constant, where I've got the name of the constant as a string attached to a variable, and then want to use that variable to call the constant.

Any way to do this?
 
Yep, that's what I'm talking about, but when you echo the variable, it won't output the constant value.

Actually, that's not quite what I'm doing. What you do works as it should. What I'm doing is like this:

define('_CONSTANT','hey');

$a = 'CONSTANT';

$b = '_'.$a;

echo $b;

That will output the string '_CONSTANT', not the constant value 'hey'. I want it to output that value.
 
seems like none of the dynamic variable techniques work for constants

might wanna mess around with eval, maybe something will work
good luck
 
Well, this was so simple I feel stupid.

define('_CONSTANT','hey');

$a = STANT;

$b = '_CON'.$a;

echo constant($b); - outputs 'hey'
 
Back
Top