How can I get PHP to evaluate a static variable in double quotes?
I want to do something like this:
log("self::$CLASS $METHOD entering");
//define below
function EXPR($v) { return $v; }
$E = EXPR;
//now you can use it in string
echo "hello - three is equal to $E(1+2)";
Unfortunately there is no way how to do this yet. Example in one of answers here will not work, because {${self::$CLASS}}
will not returns content of self::$CLASS
, but will returns content of variable with name in self::$CLASS
.
Here is an example, which does not returns myvar
, but aaa
:
$myvar = 'aaa';
self::$CLASS = 'myvar';
echo "{${self::$CLASS}}";
Sorry, you can't do that. It only works for simple expressions. See here.