引用 php 中的常量:“这是一个 MY_CONSTANT";

2022-01-23 00:00:00 constants php quoting

我想在 PHP 中使用一个常量,但我也想像变量一样将它放在双引号内.这有可能吗?

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

显然输出Hello, TESTER",但我真正想要的是这样的:

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

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

输出Hello, World!".

outputs "Hello, World!".

推荐答案

抱歉,这不是 PHP 中常量的工作方式.您可以将变量放在双引号和 heredocs 中,但不能放在常量中.

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

相关文章