Web Development – HTML Inside or Outside PHP Code?

htmlPHPweb-development

Look at this:

<?php
echo "Hello World";
?>
<br />
<?php
echo "Welcome";
?>

And now look at this:

<?php
echo "Hello World";
echo "<br />";
echo "Welcome";
?>

Which one of the above examples is considered to better (at least from performance point of view)?.

I know the examples are trivial but I want to follow good practice habits from the beginning so when I have more and more lines they would not affect the performance or whatever in a negative way.

Best Answer

As comment points out, it should be done by templates. But if only your two choice is possible. Case 1 will be better. This is the same concept as android inflating layout by xml , or programmaticlly making the UI. Your <br/> is a static content from your html and all your server will be doing is to show it. But if you use php, to execute it, this will be using your servers resources and be computing.

This is how it should be done: index.php:

<?php
include 'header.html';
...
include 'footer.html';  
?>  

header.html and footer.html are templates.

Related Topic