quoting constants in php: "this is a MY_CONSTANT"
Asked Answered
J

7

26

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!".

Jubal answered 14/10, 2009 at 0:16 Comment(1)
C
20

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

Clypeus answered 14/10, 2009 at 0:22 Comment(1)
Constants does not work's in this way. But there must be a solution for this........Foretopgallant
A
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.
Anyways answered 14/10, 2009 at 0:33 Comment(3)
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!)Electromechanical
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.Becky
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.Dysphemia
S
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);
Shannan answered 14/10, 2009 at 1:18 Comment(3)
Regex isn't necessary. str_replace() would do the same.Gaskins
@Gaskins Resurrection of a 13 year old post to comment on the way I mentioned not to do it? Yes, you are also right.Shannan
Post age is never important while I comb and curate better content to improve the researcher experience.Gaskins
D
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

Diadem answered 18/1, 2021 at 11:11 Comment(4)
I get a Uncaught Error: Function name must be a string error.Mayemayeda
@S.W.G., I have no idea why, try it here: sandbox.onlinephpfunctions.com?s=s7EvyCjg5dLXV0itSMwtyElVSM5PSe…Diadem
I had to concatenate using . instead, bu the idea to be able to sew constants is still preferred.Mayemayeda
5 years earlier: https://mcmap.net/q/479082/-constants-inside-quotes-are-not-printedGaskins
V
1

no way, unless you write your own string parsing function

Verecund answered 14/10, 2009 at 0:18 Comment(0)
G
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.

Gaiety answered 14/10, 2009 at 0:26 Comment(0)
L
0

Alternatively, do

"this is " . MY_CONSTANT

or

"this is " . constant("MY_CONSTANT");
Locker answered 2/9, 2019 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.