문제

That title probably doesn't help much. I'm trying to figure out if there's a way to get the value of a variable that has been set using define(...), but using a 2nd variable to build the defined var's name. Example will be clearer:

define('I_LOVE_YOU', 'xoxox');
$n = 'LOVE';

// how to get 'xoxox', using $n?  This won't work:
$defd = 'I_'.$n.'_YOU';
echo $defd;  // obviously echos 'I_LOVE_YOU', not 'xoxox'

// this will, but is awful
eval('echo I_'.$n.'_YOU;');  // echos 'xoxox'

Is there any other way to do this, w/o resorting to eval?

도움이 되었습니까?

해결책

Don't use eval(), use constant():

define('I_LOVE_YOU', 'xoxox');
$n = 'LOVE';
echo constant('I_'.$n.'_YOU');
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top