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?

nico
  • 50,859
  • 17
  • 87
  • 112

4 Answers4

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>
István Ujj-Mészáros
  • 3,228
  • 1
  • 27
  • 46
  • 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? – nico Dec 19 '10 at 10:59
  • edited your code a bit to meet modern standards. short tags also helps a lot – Your Common Sense Dec 19 '10 at 11:00
  • 1
    @Col. Shrapnel: yes, I like to know how things work. :) Jokes aside, it's mostly a theoretical question, nothing more. – nico Dec 19 '10 at 11:03
  • @Col. Shrapnel I don't like short tags, but agree on `foreach()`, I just rewrote the OP's code line-by-line. – István Ujj-Mészáros Dec 19 '10 at 11:04
  • @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. – Your Common Sense Dec 19 '10 at 11:08
  • @Col. Sharpnel: a link to the exact page where that is explained would be much appreciated, I wasn't really able to find it. Thank you. – nico Dec 19 '10 at 11:17
  • hmmmm that doesn't really answer my question, but thanks anyway. – nico Dec 19 '10 at 11:36
  • @nico I think readability and maintability is more important than speed, but fortunately the more readable is the fastest as well. Plain HTML: `0.00018µs`; your first PHP: `0.00062µs`; your second PHP version: `0.00063µs`; My version with the alternative syntax (foreach version): `0.00025µs`. – István Ujj-Mészáros Dec 19 '10 at 12:02
  • @Col. Shrapnel I think the difference is big enough to simply state that the alternative syntax is faster, but as I wrote, it doesn't matter at all, because readability and maintainability is more important here. – István Ujj-Mészáros Dec 19 '10 at 16:18
  • You are wrong. You don't take into account many real life factors. – Your Common Sense Dec 19 '10 at 16:26
  • @Col. Shrapnel Could you please write some example? – István Ujj-Mészáros Dec 19 '10 at 16:48
  • run apache benchmark for this test. it's much closer to the real life. then see if you can discover any difference. then add some more life to your lame environment, things like network latency, output buffering, opcode caching. – Your Common Sense Dec 19 '10 at 18:57
  • @Col. Shrapnel Xdebug caused the big difference, but the alternative syntax is still faster (~15%). – István Ujj-Mészáros Dec 19 '10 at 19:30
  • it's not xdebug I've been talking about. see, you get numbers, different every time, but understand none of it. BTW, "alternative syntax" has absolutely nothing to do here. you're talking of escaping from HTML. – Your Common Sense Dec 19 '10 at 19:43
  • @Col. Shrapnel *"you get numbers, different every time"* - thats not true. The result is the same, just the ratio is different. Thats enough for some (including me), but you are free to make more tests (just don't forget to post the results). – István Ujj-Mészáros Dec 19 '10 at 20:01
  • oh yeah. it was around 60% and now become 15%. the same result, my ass. – Your Common Sense Dec 19 '10 at 20:07
  • @Col. Shrapnel My statement was *"fortunately the more readable is the fastest as well"*. It's still the fastest, but you are free to refute me with some more testing. – István Ujj-Mészáros Dec 19 '10 at 20:59
  • I've told you already how to test. emulate (or measure) real request to the real script. then you will see that there is NO difference. – Your Common Sense Dec 20 '10 at 12:45
  • @Col. Shrapnel Yes, I am sure that we can't measure any difference on real scripts with network latency, when there is only microsecond difference exists in a small part of it. Thats why I just tested this small script that way. – István Ujj-Mészáros Dec 20 '10 at 17:22
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.

Bjoern
  • 15,934
  • 4
  • 43
  • 48
  • 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. – nico Dec 19 '10 at 11:02
  • Aye, thats why I gave a very general reply! ;) – Bjoern Dec 19 '10 at 11:04
1

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

Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
  • 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. `` will work in all cases. – George Cummins Jun 25 '13 at 16:52
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.

Dimash
  • 581
  • 1
  • 5
  • 13