views:

199

answers:

6

I want to use a constant in php, but i also want to put it inside double quotes like a variable. Is this at all possible?

define("TESTER", "World!");
echo "Hello, TESTER";

obviously outputs "Hello, TESTER", but what I really want is something like:

$tester = "World!";
echo "Hello, $tester";

ouputs "Hello, World!".

+1  A: 

no way, unless you write your own string parsing function

stereofrog
+3  A: 

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

Dinah
A: 

Why don't you just use concatenation...?

nasufara
A: 

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

joebert
+3  A: 

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.
Cesar
4. It's easier to put HTML in strings because you don't have to keep escaping the double quotes. (I know you can single-quote attribute values but the OCD in me hates that!)
DisgruntledGoat
1. Depends on syntax highlighting. 2. The opposite is true in some environments and versions (in 6, it is rumored to be just as fast or faster). 3. Valid.
Justin Johnson
1. Visibility is only poor if you're using using a very basic text editor. Personally I find excess syntax leads to bad visibility. 2. Perhaps a slower parse step in some versions, same execution speed.
Matthew
A: 

Concatenation is the way to go.

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

echo preg_replace("/TESTER/",TESTER,$original_content);
Daren Schwenke