I’m rather new to programming and i know how to separate PHP from HTML, but i would like to know if there is any difference in doing
this:
<?php $rand="I love apples" ?> <h1>This is a title</h1> <div> <p>This is a paragraph</p> <?php echo"The variable contains the string $rand"; ?> </div> ?>
compared to doing this:
<?php echo "<h1>This is a title</h1>"; echo "<div>"; echo "<p>This is a paragraph</p>"; echo "The variable contains the string $rand"; echo "</div>"; ?>
Is there any difference between in performance etc, between splitting the PHP code from the HTML code and just echoing the whole page in php?
Advertisement
Answer
The best practice is not to seperate PHP from HTML, the best practice is to seperate logic from markup.
Also important is coding style. Proper line indentions. Using echo "</div>";
instead of echo"</div>";
, valid HTML, not putting variables into quotations:
echo "The variable contains the string $rand";
better (why? see my comment below):
echo "The variable contains the string ", $rand, " :-)";
Your whole project gains much quality and worthness just by improving the code, writing clean, readable, maintainable. Imagine you want to change the Text, you would have to add or change lots of echo
es.
Code Style Guides > Pear, PSR, Zend <
encourage developers to keep their code readable, valid and cross-browser compatible