Search code examples
phphtmlcssstringheredoc

Using a php code in a variable with multiple line strings


How to give the content of $topic_image to a variable like this at the source part? The problem is here: --> src="image/<?php echo $topi_image?>" .

$display_content=<<<END_OF_TEXT
        <div>
        <img src="image/<?php echo $topic_image ?>" style="width:200px;height:150px;">
        <p style="float:left">$topic_pris</p>
        <p style="float:left">$topic_title</p>
        <p style="float:left">$topic_name</p>
        </div>
    END_OF_TEXT;

Solution

  • Heredoc expands variables itself. You don't need the whole <?php echo $topic_image ?>, just the variable:

    $display_content=<<<END_OF_TEXT
        <div>
        <img src="image/${topic_image}" style="width:200px;height:150px;">
        <p style="float:left">$topic_pris</p>
        <p style="float:left">$topic_title</p>
        <p style="float:left">$topic_name</p>
        </div>
    END_OF_TEXT;