define and heredoc

丶灬走出姿态 提交于 2019-11-29 10:12:41

taken from the documentation regarding strings

DEFINE('PREFIX','/holiday');

$const = PREFIX;

echo <<<EOD
<img src="{$const}/images/hello.png" /> 
EOD;

if you have more than 1 constant, variable usage would be difficult. so try this method

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;

http://codepad.org/lA8L2wQR

Constants used within the heredoc syntax are not interpreted!

Editor's Note: This is true. PHP has no way of recognizing the constant from any other string of characters within the heredoc block.

Source

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!