Creating a variable from a large, complicated section of html

Issue

I have several large (200+ lines) of html that I need to place in a variable to use several times in a loop. It gets very messy to escape this and that and keep formatting clean. Is there anyway to set a variable like this IF statement?

<?php if($condition): ?> <p>html here</p> <?php endif; ?>

I know its possible with external files, but Id rather not add a series of includes to achieve this.

Solution

Use heredoc notation:

$html = <<<HTML
<div class="section">
    <h2>Header</h1>
    <p>Paragraph 1</p>
    <p>Paragraph 2</p>
</div>
HTML;

No worrying about special characters, and you can reuse the variable as much as you want.

Answered By – Josh Leitzel

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published