定义和heredoc

moe*_*oey 8 php heredoc

你如何在heredoc中使用define?例如:

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)


MC_*_*a_T 6

如果你有超过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)

http://codepad.org/lA8L2wQR