Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 in PHP and echo HTML like so:

<?php
  doSomething();
  echo "<div id=\"some_div\">Content</div>";
?>

Or have a HTML file like so and just add in the PHP:

<html>
<body>
  <?php doSomething(); ?>
  <div id="some_div">Content</div>
</body>
</html>

It seems tidier to echo HTML, especially if lots of PHP gets used throughout the page, but doing so loses all formatting of the HTML i.e. colors in the IDE etc.

like image 383
Adam Holmes Avatar asked Nov 10 '10 19:11

Adam Holmes


People also ask

How can I use HTML and PHP together?

Using these simple steps, we can easily add the PHP code. Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to use the PHP. Step 2: Now, we have to place the cursor in any tag of the <body> tag where we want to add the code of PHP.

When should I use PHP and HTML?

PHP is used for server-side programming which will interact with databases to retrieve information, storing, email sending, and provides content to HTML pages to display on the screen. HTML is used for specifying colors, text formatting, aligning, etc. PHP is easy to learn but not as much as HTML.

Should PHP go before or after HTML?

Add your php code before the html code. This allows you to change the out type, set requied variables, add http response headers if you require, etc. You can have a lot of php embeded tags in between the html. The html in your question would be invalid, if you echoed output before or after .

Can we use HTML inside PHP?

Yes, HTML can be embedded inside an 'if' statement with the help of PHP.


3 Answers

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.

like image 197
Pekka Avatar answered Sep 24 '22 16:09

Pekka


I think this would depend on your group's or your own decided convention. And it can and should vary depending on what type of file you're working in. If you follow the MVC pattern then your views should be the latter. If you're writing a class or some non-output script/code then you should use the former.

Try to keep a separation of display or formatting of output and the logic that provides the data. For instance let's say you need to make a quick page that runs a simple query and outputs some data. In this case (where there is no other existing infrastructure or framework) you could place the logic in an include or in the top or the bottom of the file. Example:

<?php   # define some functions here that provide data in a raw format ?> <html> <body>   <?php foreach($foo = data_function($some_parameter) as $key => $value): ?>   <p>     <?=$value;?>   </p>   <?php endforeach; ?> </body> </html> 

Or you could place the logic and function definitions in an include file or at the bottom of the file.

Now if you're producing some sort of class that has output (it really shouldn't) then you would echo the HTML or return it from the method being called. Preferably return it so that it can be output whenever and however the implementer would like.

like image 34
sholsinger Avatar answered Sep 22 '22 16:09

sholsinger


The syntax highlighting is an important benefit of the second method, as you said. But also, if you're following good practices where logic and presentation are separated, you will naturally find that your files that contain HTML are almost entirely HTML, which then, naturally, leads to your second method again. This is the standard for MVC frameworks and the like. You'll have a bunch of files that are all PHP, doing logic, and then when that's done they'll include a presentation file which is mostly HTML with a sprinkling of PHP.

like image 39
Tesserex Avatar answered Sep 26 '22 16:09

Tesserex