在字符串中包含常量

时间:2012-12-01 12:16:55

标签: php constants

在字符串中包含PHP常量是否有更短的方法?例如,而不是:

define("MYCONST","this");
echo "hello, my const is set to: " . MYCONST . ", and that is that.";

例如,我最近在字符串中包含数组时了解了{}标记,这意味着您不必逃脱。有没有类似的方法来做常量,所以我不必每次都逃避字符串包括它们?

echo "hello, my const is set to: {MYCONST}, and that is that.";

..但这似乎不起作用:(

3 个答案:

答案 0 :(得分:2)

没有。不幸的是,如果你想在其中使用常量,你必须连接字符串。

答案 1 :(得分:0)

你不能以这种方式使用常数。你需要添加它的字符串。否则,还有另一种方式:

试试这个:

define("MYCONST","this");
echo "hello, my const is set to: " , MYCONST , ", and that is that.";

输出:

hello, my const is set to: this, and that is that.

演示:http://phpfiddle.org/main/code/t63-1zx

答案 2 :(得分:0)

它不起作用{}将值转换为变量。