Will writing HTML with PHP echo vs writing simple HTML to cause any performance differences?

What is better in terms of optimizing the CPU for the web server? Writing plain HTML and pasting 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 write all the HTML echoed 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?

+6
source share
4 answers

Finally, is the difference even material or small?

It does not matter how reasonable this is, it is completely insignificant.

Of course, reasonable readability - I believe that the first block is much more readable than the second. Wrapping HTML into PHP code like this does not make sense - on the one hand, it becomes more difficult to debug.

+12
source

The first block is what can be called a php template, the second pure PHP.

It really depends on what you write more. If HTML is to be used first, if PHP is still using first, just separate it into another file and use as a template :)

+2
source

If you are writing an entire page (and complex) with an echo, you can add a bit of overhead since all of these lines must be done on the server side.

I try to stay away from the readability issue mentioned in another answer, although there may be cases (for example, being able to branch based on some values) where you might need to use this approach.

+1
source

In fact, there is no difference between the two when it comes to how the server sees this. It all comes down to how easy it would be to upgrade it. you may have problems with single and double quotes because you will need to avoid the same type of quotes that you use to contain html. as

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

This can be a pain several times on long pages.

+1
source

Source: https://habr.com/ru/post/902956/


All Articles