Jump to content

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


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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.