Reputation: 10887
How do you use define within a heredoc? For example:
define('PREFIX', '/holiday');
$body = <<<EOD
<img src="PREFIX/images/hello.png" /> // This doesn't work.
EOD;
Upvotes: 11
Views: 4844
Reputation: 616
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;
Upvotes: 7
Reputation: 455072
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.
Upvotes: 3
Reputation: 8101
taken from the documentation regarding strings
DEFINE('PREFIX','/holiday');
$const = PREFIX;
echo <<<EOD
<img src="{$const}/images/hello.png" />
EOD;
Upvotes: 11