Would writing HTML with PHP's echo vs writing plain HTML cause any differences in performance?
Asked Answered
P

4

6

Which is better in terms of CPU optimization for a web server? Writing plain HTML and inserting PHP code here and there?

<script type="text/javascript">$(document).ready(function(){$('#search').focus();});</script>
<div id="default">
    <div class="left">
        <?include(DIR_DIV.'facebook.php')?>
    </div>
    <div class="right">
        <?include(DIR_ADS.'google.300x250.php')?>
    </div>
    <div class="sep"></div>
</div>

Or writing the all the HTML with echo in PHP?

echo '<script type="text/javascript">$(document).ready(function(){$(\'#search\').focus();});</script>';
echo '<div id="default">';
    echo '<div class="left">';
        include(DIR_ADS.'google.300x250.php');
    echo '</div>';
    echo '<div class="right">';
        include(DIR_DIV.'facebook.php');
    echo '</div>';
    echo '<div class="sep"></div>';
echo '</div>'

Does the difference even matter or is it insignificant?

Piecrust answered 2/12, 2011 at 18:31 Comment(0)
B
14

finally, does the difference even matter or is it insignificant?

It doesn't matter performance wise, it's completely insignificant.

It does matter readability wise though - I find the first block far more legible than the second one. Wrapping HTML into PHP code like that doesn't make sense - it becomes harder to debug, for one thing.

Breechblock answered 2/12, 2011 at 18:33 Comment(0)
M
2

First block is what might be called php template, second pure PHP.

It really depends on what you will be writing more. If HTML - use first, if PHP still use first, just separate it to different file and use as template :)

Middlesworth answered 2/12, 2011 at 18:36 Comment(0)
C
1

If you were writing an entire page (and a complex one) with echo you may add a little bit of overhead, since all those lines would need to be executed server side.

I try to stay away from that for the readability issue mentioned in the other answer, though there may be cases (like having to branch based on some values) where you may need to use this approach.

Caston answered 2/12, 2011 at 18:37 Comment(0)
G
1

There is really no difference between the two when it comes to how the server will see it. It all comes down to how easy it would be to update it. you can have issues with single and double quotes because you would need to make sure to escape the same type of quotes that you are using to contain the html. like

echo " <span class=\"first\">".$first."</span>";

This can be a pain some times in long pages.

Greathouse answered 2/12, 2011 at 19:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.