开发者

Fastest method for outputting static HTML in PHP

As I've started to use PHP more, I've noticed two techniques for outputting static HTML content. I'm curious which is the faster/more efficient method. Essentially, one uses PHP whereas the other doesn't. I suppose it might hinge on whether it puts extra strain on the server to constantly go in and out of php.

In general, is one of these methods more efficient?

Method 1:

<?php
$muniResult = mysql_query("SELECT muni.full, muni.pk FROM muni ORDER BY开发者_JAVA技巧 muni.full ASC");
$munis = mysql_num_rows($muniResult);
?><select><?php
while ($munis > 0) {
    $thisEntry = mysql_fetch_array($muniResult);
    ?><option value="<?php echo $thisEntry['pk']; ?>"><?php echo $thisEntry['full']; ?></option><?php
    $munis--;}
}
?>
</select>

Method 2:

<?php
$muniResult = mysql_query("SELECT muni.full, muni.pk FROM muni ORDER BY muni.full ASC");
$munis = mysql_num_rows($muniResult);
echo "<select>";
while ($munis > 0) {
    $thisEntry = mysql_fetch_array($muniResult);
    ?>
    echo "<option value=\"".$thisEntry['pk']."\">".$thisEntry['full']."</option>";
    $munis--;
}
echo "</select>";
?>


If you benchmark them, you will see that using echo (Method #2) is actually slower than outputting HTML outside the interpreted code. The performance is still negligible.

However, I'm in favour of the latter (i.e.: not using echo). It is a question of maintainability.

Say you have this:

<?php
echo '<select>';
foreach ($options as $option) {
    echo "<option>$option</option>";
}
echo '</select>';
?>

Versus this:

<select>
<?php foreach ($options as $option): ?>
   <option><?php echo $option; ?></option>
<?php endforeach; ?>
</select>

I find the latter more readable and easier to maintain the former. Some will disagree, as it is more a matter of personal preference. However, I found more people that agree with me than those who don't.

Note: Your code doesn't parse. ;)


Both of these methods use PHP, and neither of them would be significantly faster than the other, the entire text is parsed by the preprocessor, before being sent to HTTP.


I have never run a benchmark on this but my personal opinion is that the difference is negligible.

What is better than both of those methods however is doing all your processing first, then printing the results into the HTML. A common method is to use a templating tool like Smarty. The reason for this is because you should almost always separate your concerns when programming applications, especially web applications. You should make sure that the data processing is separate from the application logic and also from application design. This is called Model View Controller (MVC) and is a great way to keep your application organised and maintainable. If that's a bit too complicated for you, then you should at least try out Smarty. It's very easy to use and will save you a lot of time and effort.

Edit: Also, you should never print user-supplied data in your html without first escaping it. This is to prevent Cross Site Scripting (XSS) which is a common yet very serious vulnerability in web pages. Rather than doing this:

<?php echo $thisEntry['pk']; ?>

You should use the htmlspecialchars function:

<?php echo htmlspecialchars($thisEntry['pk'], ENT_QUOTES, 'UTF-8'); ?>

You should replace the 'UTF-8' with the correct encoding of your data. This is the reason you should also explicitly state the encoding: http://shiflett.org/blog/2007/may/character-encoding-and-xss

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜