摘自字符串相关文档
DEFINE('PREFIX','/holiday');
$const = PREFIX;
echo <<<EOD
<img src="{$const}/images/hello.png" />
EOD;
define('PREFIX', '/holiday');
define('SUFFIX', '/work');
define('BLABLA', '/lorem');
define('ETC', '/ipsum');
$cname = 'constant'; // if you want to use a function in heredoc, you must save function name in variable
$body = <<<EOD
<img src="{$cname('PREFIX')}/images/hello.png" />
<img src="{$cname('SUFFIX')}/images/hello.png" />
<img src="{$cname('BLABLA')}/images/hello.png" />
<img src="{$cname('ETC')}/images/hello.png" />
EOD;
在heredoc语法中使用的常量不会被解释!
编辑说明:这是真的。PHP无法识别heredoc块中的常量与其他字符串。
$const/images/hello.png
也可以使用。 - moey$consts = get_defined_constants();
来获取所有的定义,然后通过{$consts['PREFIX']}
访问。 - PhoneixS