How to access php specific constant using $ variable as the name of a specific constant

I have a variable defined using define (). I want to save part of the name of this variable in a regular php $ variable, then enter this variable by specifying it dynamically. i.e:.

define('xxx_yyy',123);
$a='xxz';
$b='_yyy';


//How to I echo out "123" now?  (without using echo xxx_yyy);
//Something like these (don't work):
echo $a$b;
echo {$a$b};

The only thing I can think of:

$defined=get_defined_vars();
echo $defined[$a$b];

but it seems awkward

+3
source share
5 answers
echo constant ( $a . $b );

- this is what I think you are looking for, as it is permanent.

+9
source
+2
source

, constant:

echo constant ( $a . $b );
+2

- get_defined_constants(), get_defined_vars().

0

- 123, :

echo $a . $b;

, define() .

-4

All Articles