Heavy use of PHP's "<?php" tag

As far as readability goes, I would rather see:

  <ul>
      <?php foreach ($items as $item): ?>
          <li>
              <a href="<?php esc($item->url)?>">
              <img src="<?php esc($item->icon)?>"/>
              <?php esc($item->text)?>
          </li>
      <?php endforeach; ?>
  </ul>

Than:

   echo "<ul>";
   foreach ($items as $item)
   {
     echo "<li>";
     echo '<a href="'.esc($item->url).'">';
     echo '<img src="'.esc($item->icon).'"/>';
     echo esc($item->text);
     echo '</li>';
   }
   echo "</ul>";

Not only that, but the latter lets your IDE of choice handle the HTML syntax and formatting (telling you, for instance, that the </a> is missing). So, unless there's a lot more going on in-between the short bits of HTML, <?php might be preferable.

EDIT: as for performance, anyone serious about code speed will activate a caching pre-compiler which will boil down both versions to the exact same thing.


No reason whatsoever apart from its beginners scripting, there just trying to get the results to the page without any architectural thinking or planning into the system into the long run.

What you should be doing is splitting your design up away from your logical php code, and the design compilation should be done at the end of the scripts runtime.

if you redesign the application I would certainly advise you to start with a framework because the framework will force bad habits away by its design.

Start with codeigniter and create a simple blog, understand how to connect/insert/select/update with the database, learn how to handle sessions, learn the Controllers and the principles of creating one.

After you have had a decent play about with it start looking at the poorly coded applicatioon from a distance not looking at the code or the design but yet what exactly is it doing, is it fetching results from the database, does it have a user system etc etc.

then start implementing the base layer of the application such as the above, once you have the database designed you can then start building the models to fetch from the database at the point within your application, start creating the basic view files taking samples from the pooorly coded application and recoding them within the new application, keeping in mind the structure and cleanliness of the coding.

Hope this helps you start migrating because I certainly do not advise you continue to work with an application such as that.


@mitch

Event thought you second piece of code is cleaner its still combining your view with the rest of your application where it should be like so:

<html>
    <?php $this->load("segments/head"); ?>
    <body>
         <?php echo $this->wrap("span",$this->link("Some Linke",$this->var("homepage"))) ?>
    </body>
</html>

a dedicated set of methods for the view to prevent it interacting with the main logic, this would be wrapped within an object to prevent the scope and only the object should be able to access the main logic.


Reason it might be like that is for situations like this:

<?php if($logged_in) { ?>
    <span class="x">Welcome, <?= $_SESSION['user'] ?></span>
    <a href="logout.php">Logout</a>
<?php } ?>

Instead of:

<?php 
   if($logged_in) {
      echo "<span class=\"x\">Welcome, " . $_SESSION['user'] . "</span>";
      echo "<a href=\"logout.php\">Lougout</a>";
   }
?>

There are less escape characters to worry about.