Search code examples
phphtmlheredoc

here document with HTML not identifying delimiter


What is supposed to happen when there is HTML in a here document? Isn't it supposed to just be stored as a string? The HTML in this screen is displayed on screen and so is the delimiter "HTMLBLOCK;" portion of the code. What is going on?

<? php
<<<HTMLBLOCK
<html>
<head><title>Menu</title></head>
<body bgcolor="#fffed9">
<h1>Dinner</h1>
<ul>
<li>Beef Chow-Mun</li>
<li>Sauteed Pea Shoots</li>
<li>Soy Sauce Noodles</li>
</ul>
</body>
</html>
HTMLBLOCK;
?>

OUTPUT

Dinner

Beef Chow-Mun

Sauteed Pea Shoots

Soy Sauce Noodles

HTMLBLOCK; ?>

Solution

  • Your PHP does have a wrong syntax on the top with: <? php. There should be no space there. Instead of doing that, you could echo the HTML content with PHP; or you can close out of PHP and write out the PHP then open PHP tags again:

    echo '<html>
        <head><title>Menu</title></head>
        <body bgcolor="#fffed9">
            <h1>Dinner</h1>
            <ul>
                <li>Beef Chow-Mun</li>
                <li>Sauteed Pea Shoots</li>
                <li>Soy Sauce Noodles</li>
            </ul>
        </body>
    </html>';
    

    Or you could do this:

    <?php
    // .. PHP code here .. 
    ?>
    <html>
        <head><title>Menu</title></head>
        <body bgcolor="#fffed9">
            <h1>Dinner</h1>
            <ul>
                <li>Beef Chow-Mun</li>
                <li>Sauteed Pea Shoots</li>
                <li>Soy Sauce Noodles</li>
            </ul>
        </body>
    </html>
    <?php
    // ..code continues..
    ?>
    

    You could also store the HTML in a variable and `echo the variable.