简体   繁体   English

定义和heredoc

[英]define and heredoc

How do you use define within a heredoc ? 你如何在heredoc中使用define For example: 例如:

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

$body = <<<EOD
<img src="PREFIX/images/hello.png" />   // This doesn't work.
EOD;

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. 如果你有超过1个常数,那么变量的使用会很困难。 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 http://codepad.org/lA8L2wQR

Constants used within the heredoc syntax are not interpreted! heredoc语法中使用的常量不会被解释!

Editor's Note: This is true. 编者注:这是事实。 PHP has no way of recognizing the constant from any other string of characters within the heredoc block. PHP无法识别heredoc块中任何其他字符串的常量。

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM