24

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";

outputs "Hello, World!".

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
helloandre
  • 10,541
  • 8
  • 47
  • 64
  • 1
    [here's a clue about another interesting way to expand CONSTANTS in php heredoc notation](http://www.php.net/manual/en/function.define.php#100449) – Scott Evernden Dec 30 '11 at 21:44

7 Answers7

20

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

Dinah
  • 52,922
  • 30
  • 133
  • 149
  • 2
    Constants does not work's in this way. But there must be a solution for this........ – Bik Jun 11 '14 at 08:08
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.
Cesar
  • 3,519
  • 2
  • 29
  • 43
  • 2
    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 Oct 14 '09 at 00:55
  • 1
    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 Oct 14 '09 at 01:00
  • 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 Oct 14 '09 at 01:30
5

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
  • 5,428
  • 3
  • 29
  • 34
3

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

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

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

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

Tested in: PHP 5.6, 7.4.27, 8.1

  • I get a `Uncaught Error: Function name must be a string ` error. – S. W. G. Mar 30 '22 at 11:26
  • @S.W.G., I have no idea why, try it here: https://sandbox.onlinephpfunctions.com?s=s7EvyCjg5dLXV0itSMwtyElVSM5PSeXlSklNy8xL1VB3dHRU11FQNzYIU9e05uVSSc7PKy5JzCtRsFVQh7HVgRKpyRn5CkrVcHmITs1aJWsFfX1XoGRqsQLQEF4uAA%2C%2C&v=5.6.29 – AlexisAmasis Mar 31 '22 at 11:02
  • I had to concatenate using . instead, bu the idea to be able to sew constants is still preferred. – S. W. G. Mar 31 '22 at 11:14
  • 1
    5 years earlier: https://stackoverflow.com/a/35356682/2943403 – mickmackusa Apr 28 '22 at 04:09
1

no way, unless you write your own string parsing function

user187291
  • 53,363
  • 19
  • 95
  • 127
1

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
  • 2,653
  • 2
  • 18
  • 22
0

Alternatively, do

"this is " . MY_CONSTANT

or

"this is " . constant("MY_CONSTANT");
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129