[ACCEPTED]-quoting constants in php: "this is a MY_CONSTANT-quoting

Accepted answer
Score: 20

Sorry, that's not the way constants in PHP 2 work. You can put variables in double quotes 1 and heredocs but not constants.

Score: 6

I recomend you to use concatenation because:

  1. When you use a variable into a double quotes string your visibility is not good;
  2. When you use a double quotes string the php can to process slowly;
  3. You don't use a constant into a string, because don't have any delimiter to the php knows what is the constant.


Score: 5

Concatenation is the way to go.

Unless you 2 want the hokey, nasty, inefficient, evil 1 monkey way of:

echo preg_replace("/TESTER/",TESTER,$original_content);
Score: 3

Posting for anyone who might find it useful, something 6 like this will work:

// example code
define('AAA', '30V');
$constant = 'constant';
echo "{$constant('AAA')}"; //Echoes 30V

Trust me, neither I 5 believed something like this was possible, but 4 it is! Try it in tehplayground.com and see 3 for yourself.

Credit goes to whoever though 2 of it, and to my colleague who found it 1 (I think he did in stackoveflow).

Score: 1

no way, unless you write your own string 1 parsing function

Score: 1

I've found that when dot-concatenation of 3 a constant is a problem, using sprintf to 2 get my string is usually the way I want 1 to go in the end.

Score: 0

Alternatively, do

"this is " . MY_CONSTANT


"this is " . constant("MY_CONSTANT");


More Related questions