开发者

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

Some PHP code I look at is littered with "<?php" and "?>" tags depending on whether it's outputting HTML or not. Is there any performance benefit to this rather than using echo to write the HTML? It makes the code extremely hard to read when the code's constantly switching between code and HTML via the "<?php" tag.

Note that I'm not just talking about the occasional switchover. The code I'm currently looking at (the mantis-bt source co开发者_如何学JAVAde) is giving me a headache with the number of times it's switching. Very very hard to read.

I wonder if there's a reason for them doing it like this?


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.


While this would not produce any noticeable effects as far as code run time, the idea that this is the only place that "Performance" should be calculated, is ridiculous. Developers cost money. So cleaning this all up IS a performance boost! Your own performance!

So do it.


PHP offers some features that are not offered by HTML , such as loops. Thus, if you have to use loops several times in your code, you have to keep switching between php tags and HTML. Moreover, PHP helps you implement session control that is not allowed by HTML. Thus, it is important to embed PHP in HTML

In a nutshell, to use additional features we have to use PHP or some other language


I don't have statistical data to back this up, but it's my understanding that it's more efficient to "turn off" php to output HTML rather than to use echo""; The reason being that when you run your html through echo tags, you're having PHP parse it out to display, while just putting it in the doc itself will let the browser display it WITHOUT having to have PHP parse it.

When I did ColdFusion development, I remember hearing the same case made for tags.


I prefer to use the <?php and ?> because it is easier to read HTML, but if any loss of performance will be very little. I've written HTML with echo but it's very bad to read and find problems in the HTML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜