Jump to content

Is It Possible To Put Into A Php Variable An HTML Piece Of Code Built From Plain Text And A Variable?


phdphd

Recommended Posts

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!

 

 

 

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;

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.