Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use constants with Complex (curly) syntax?

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.

like image 991
aland Avatar asked Feb 04 '23 02:02

aland


2 Answers

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?";
like image 58
Sarfraz Avatar answered Mar 01 '23 17:03

Sarfraz


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?";
like image 30
bcosca Avatar answered Mar 01 '23 15:03

bcosca