Skip to content
Advertisement

Creating a variable from a large, complicated section of html

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement