phdphd Posted December 10, 2013 Share Posted December 10, 2013 Hi All, The heredoc syntax enables to put some text in a variable for future reuse. For example : $my_var = <<<EOD The colour is red. EOD; echo $my_var; will result in "The colour is red". So far so good. Now let's assume the following: $colour="red"; $my_var = <<<EOD echo 'The colour is '.$colour; EOD; echo $my_var; Unfortunately the result will be "echo 'The colour is '.red;". In this situation, is there a way to tell php to just put "The colour is red" into the $my_var variable ? Or is there an alternative to heredoc ? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/284694-is-it-possible-to-put-into-a-php-variable-an-html-piece-of-code-built-from-plain-text-and-a-variable/ Share on other sites More sharing options...
requinix Posted December 10, 2013 Share Posted December 10, 2013 Heredoc is a string. You don't put PHP code into it. $my_var = "The colour is red"; $my_var = <<<EOD The colour is red EOD; $colour = "red"; $my_var = <<<EOD The colour is $colour EOD; Quote Link to comment https://forums.phpfreaks.com/topic/284694-is-it-possible-to-put-into-a-php-variable-an-html-piece-of-code-built-from-plain-text-and-a-variable/#findComment-1462018 Share on other sites More sharing options...
phdphd Posted December 10, 2013 Author Share Posted December 10, 2013 Thanks for your answer. Quote Link to comment https://forums.phpfreaks.com/topic/284694-is-it-possible-to-put-into-a-php-variable-an-html-piece-of-code-built-from-plain-text-and-a-variable/#findComment-1462019 Share on other sites More sharing options...
grissom Posted December 11, 2013 Share Posted December 11, 2013 I would just do : $colour="red";$myvar = 'The colour is '.$colour; This looks a bit too easy ! Have I misunderstood the question ? Quote Link to comment https://forums.phpfreaks.com/topic/284694-is-it-possible-to-put-into-a-php-variable-an-html-piece-of-code-built-from-plain-text-and-a-variable/#findComment-1462120 Share on other sites More sharing options...
requinix Posted December 11, 2013 Share Posted December 11, 2013 I would just do : $colour="red"; $myvar = 'The colour is '.$colour; This looks a bit too easy ! Have I misunderstood the question ? Nope. Was just a question about syntax. Quote Link to comment https://forums.phpfreaks.com/topic/284694-is-it-possible-to-put-into-a-php-variable-an-html-piece-of-code-built-from-plain-text-and-a-variable/#findComment-1462127 Share on other sites More sharing options...
ertuncefeoglu Posted December 12, 2013 Share Posted December 12, 2013 Have a look at nowdoc syntax - especially the stuff mentioning double quotes (PHP 5.3.x and later) http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc Quote Link to comment https://forums.phpfreaks.com/topic/284694-is-it-possible-to-put-into-a-php-variable-an-html-piece-of-code-built-from-plain-text-and-a-variable/#findComment-1462190 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.