What is the best practice to use when using PHP and HTML?

后端 未结 9 910
梦毁少年i
梦毁少年i 2020-11-30 04:52

I have been designing websites for a while now, but there is one thing that I have never been quite sure of when using PHP and HTML. Is it better to have the whole document

相关标签:
9条回答
  • 2020-11-30 05:39

    The second method is what I usually use. And it was the default method for me too. It is just to handy to get php to work inside html rather than echo out the html code. But I had to modify the httpd.conf file as my server just commented out the php code.

    0 讨论(0)
  • 2020-11-30 05:41

    There are varying opinions on this. I think there are two good ways:

    • Use a templating engine like Smarty that completely separates code and presentation.

    • Use your second example, but when mixing PHP into HTML, only output variables. Do all the code logic in one block before outputting anything, or a separate file. Like so:

      <?php $content = doSomething();
         // complex calculations
      ?>
      <html>
      <body>
        <?php echo $content; ?>       
        <div id="some_div">Content</div>
      </body>
      </html>
      

    Most full-fledged application frameworks bring their own styles of doing this; in that case, it's usually best to follow the style provided.

    0 讨论(0)
  • 2020-11-30 05:45

    Simple:

    More PHP - close HTML in PHP. When you generate HTML code in PHP, when you are doing something like a class, so it is better to make it in echo.

    Less PHP - close PHP in HTML. This is stuff like just putting vars into fields of HTML stuff, like forms... And such.

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