views:

41

answers:

2

I was surprised to see that the following doesn't work as expected.

define('CONST_TEST','Some string');
echo "What is the value of {CONST_TEST} going to be?";

outputs: What is the value of {CONST_TEST} going to be?

Is there a way to resolve constants within curly braces?

Yes, I am aware I could just do

echo "What is the value of ".CONST_TEST." going to be?";

but I'd prefer not to concatanate strings, not so much for performance but for readability.

+3  A: 

Nope that's not possible because php will consider CONST_TEST to be a mere string inside the single/double quotes. You will have to use the concatenation for that.

echo "What is the value of ".CONST_TEST." going to be?";
Sarfraz
Thanks for the explaination, I thought php would resolve everything inside curly braces.. but I see that only works for $ variables.
aland
Yep. It will only work for something starting with a $. E.g. `{Class::static}` doesn't work either :(
nikic
+1  A: 

i don't understand why you have to make a big fuss out of it but you can always do:

define('CONST_TEST','Some string');
$def=CONST_TEST;
echo "What is the value of $def going to be?";
stillstanding
I didn't think I was making a big fuss, just curiousity :)
aland