nico
nico

Reputation: 51640

Separating PHP code from HTML output

Very often I have heard people suggesting (and I have done it myself too a few times) to keep things separate: PHP code here, HTML there, external CSS, external JS and so on and so on.

Aside from the obvious readibility and maintenance advantages of doing this are there other strong advantages (e.g. in terms of server load or page processing time) in doing it?

As a trivial example, say we want to implement a table containing some products we read from a DB.

The output we want would be something like

<div class="description">This table lists all our products</div>
<table class="products">
   <tr>
     <th>Name</th>
     <th>Available</th>
     <th>Price</th>
   </tr>
   <tr>
     <td>Prod 1</td>
     <td>Yes</td>
     <td>$100</td>
   </tr>
  ...
  ...
  </table>
  <div class="info">Some generic info on the products here</div>

So here we have some static output (the 2 div elements and the table header) and some dynamic output (the actual table content).

We could leave all the static things out of PHP tags and try to keep PHP only where needed

 <div class="description">This table lists all our products</div>
 <table class="products">         
 <tr>
   <th>Name</th>
   <th>Available</th>
   <th>Price</th>
 </tr>
 <?
 for ($p=0; $p<count($products); $p++)
      {
      echo '<tr>';
      echo '<td>'.$products[$p]["name"].'</td>';
      echo '<td>'.$products[$p]["availability"].'</td>';
      echo '<td>'.$products[$p]["price"].'</td>';
      echo '</tr>';
      }
 ?>
 </table>
 <div>.....</div>

On the other hand we could embed everything in PHP

 <?
 echo '<div class="description">This table lists all our products</div>';
 echo '<table class="products"><tr><th>Name</th>'.
      '<th>Available</th><th>Price</th></tr>';

 for ($p=0; $p<count($products); $p++)
      {
      echo '<tr>';
      echo '<td>'.$products[$p]["name"].'</td>';
      echo '<td>'.$products[$p]["availability"].'</td>';
      echo '<td>'.$products[$p]["price"].'</td>';
      echo '</tr>';
      }

 echo '</table>';
 echo '<div>.....</div>';

What are the reasons to choose one over the other?

Upvotes: 2

Views: 1347

Answers (4)

Dimash
Dimash

Reputation: 591

I do not suggest to use short sytax of php. Sometimes it can be problem to move code from one server to another.

Reason you need to do so is time. Nice code is simple to support and upgrade. In some cases it is performance issue also, but not in your case.

Ideal example in your case is: You have to files index.php products.php

File products.php contain

<?php
...
foreach($products as $product)
{
    $productHTML[] = '<tr><td>' . $product["name"] . '</td></tr>';
}

$productHTML = implode("", productHTML);
?>

index.php:
<html>
...
<?php echo $productsHTML;?>
...
</html>

Ofcourse more advence developers use more hard constructions, we use functions, class, template idea and etc. But such way is enough for small project.

Upvotes: 0

The alternative syntax for control structures seems to be more readable to me:

<div class="description">This table lists all our products</div>
<table class="products">         
    <tr>
        <th>Name</th>
        <th>Available</th>
        <th>Price</th>
    </tr>
<?php foreach($products as $p): ?>
    <tr>
        <td><?php echo $p["name"]; ?></td>
        <td><?php echo $p["availability"]; ?></td>
        <td><?php echo $p["price"]; ?></td>
    </tr>
<?php endforeach; ?>
</table>
<div class="info"><?php echo $info; ?></div>

Upvotes: 4

Alex Pliutau
Alex Pliutau

Reputation: 21957

I think that it is a very compact string <?= $var ?>

Upvotes: 1

Bjoern
Bjoern

Reputation: 16304

If its just a piece of code for you to play with, it doesn't really matter at all.

But if an application grows more and more complex (and more people work in it), it will usually come to a point where it is vital to separate the view layer (here: HTML) from the code layer (here: PHP) - so you can assign designers to play around with the output and coders to play around with the functionality behind it.

This ain't a php-only topic, this is very general. Architectural models like MVC are based on similar theories.

Upvotes: 1

Related Questions