How can I echo HTML in PHP?

后端 未结 13 2227
耶瑟儿~
耶瑟儿~ 2020-11-22 06:40

I want to conditionally output HTML to generate a page, so what\'s the easiest way to echo multiline snippets of HTML in PHP 4+? Would I need to use a template framework lik

相关标签:
13条回答
  • 2020-11-22 07:23

    Basically you can put HTML anywhere outside of PHP tags. It's also very beneficial to do all your necessary data processing before displaying any data, in order to separate logic and presentation.

    The data display itself could be at the bottom of the same PHP file or you could include a separate PHP file consisting of mostly HTML.

    I prefer this compact style:

    <?php
        /* do your processing here */
    ?>
    
    <html>
    <head>
        <title><?=$title?></title>
    </head>
    <body>
        <?php foreach ( $something as $item ) : ?>
            <p><?=$item?></p>
        <?php endforeach; ?>
    </body>
    </html>
    

    Note: you may need to use <?php echo $var; ?> instead of <?=$var?> depending on your PHP setup.

    0 讨论(0)
提交回复
热议问题