2

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?

3
  • 1
    Performance-wise? None. (Or, to be exact, insignificant in favor of the first one) Commented Dec 19, 2010 at 10:53
  • here is the complete example of such separation: stackoverflow.com/questions/3988627/using-template-on-php Commented Dec 19, 2010 at 11:03
  • the reason is very simple, Just open your template code in the editor with syntax highlighting. that's all. Commented Dec 19, 2010 at 11:05

4 Answers 4

4

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>
Sign up to request clarification or add additional context in comments.

19 Comments

Ok, you're hitting the right point... how is this processed? Does the PHP compiler consider all of those commands in between <? ?> as separate entities or does it all include them in one single call?
edited your code a bit to meet modern standards. short tags also helps a lot
@Col. Shrapnel: yes, I like to know how things work. :) Jokes aside, it's mostly a theoretical question, nothing more.
@Col. Shrapnel I don't like short tags, but agree on foreach(), I just rewrote the OP's code line-by-line.
@nico if you want to know how things work, there is official documentation for you. It's way more reliable source of information. This particular question being answered at the very beginning of it.
|
1

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.

2 Comments

Björn, thank you for your answer. I have obviously given a very trivial example here for the sake of simplicity, but I am interested in what happens when you scale this up.
Aye, thats why I gave a very general reply! ;)
1

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

1 Comment

It may be worth noting that the shorthand notation <?= won't work on installations where short-open tags are disabled, which is the default configuration in new versions of PHP. <?php echo $var; ?> will work in all cases.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.