define('PREFIX', '/holiday');
$body = <<<EOD
<img src="PREFIX/images/hello.png" /> // This doesn't work.
EOD;
Run Code Online (Sandbox Code Playgroud)
Jan*_*aek 10
取自关于字符串的文档
DEFINE('PREFIX','/holiday');
$const = PREFIX;
echo <<<EOD
<img src="{$const}/images/hello.png" />
EOD;
Run Code Online (Sandbox Code Playgroud)
如果你有超过1个常数,那么变量的使用会很困难.所以尝试这种方法
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;
Run Code Online (Sandbox Code Playgroud)