Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should we separate PHP from HTML

Tags:

html

php

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?

like image 953
Kenneth .J Avatar asked Dec 18 '13 17:12

Kenneth .J


People also ask

Should I separate PHP and HTML?

The most readable is php in html but only a little. You should split your file in two parts.

Can I use HTML and PHP together?

Mixing HTML and PHP. PHP code is normally mixed with HTML tags. PHP is an embedded language, meaning that you can jump between raw HTML code and PHP without sacrificing readability. In order to embed PHP code with HTML, the PHP must be set apart using PHP start and end tags.

Why use PHP not HTML?

Answer: PHP allows dynamic web page generation, which is not possible by using just HTML. Thus by using PHP script along with HTML in a PHP file, the web developers can generate dynamic web pages. The use of PHP also allows access to various databases.

Why is PHP required?

PHP (Hypertext Preprocessor) is known as a general-purpose scripting language that can be used to develop dynamic and interactive websites. It was among the first server-side languages that could be embedded into HTML, making it easier to add functionality to web pages without needing to call external files for data.


1 Answers

<?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>
?>

The above looks poorly separated. This is what php/html separation should look like:

<?php 
$rand="I love apples"; 
?>
<h1>This is a title</h1>
<div>
    <p>This is a paragraph</p>
    <p>The variable contains the string <?=$rand ?></p>
</div>

Performance-wise, that's not an issue but it would do much favor for programmers to be able to read the code easily, hence the need for HTML/PHP separation practices. Ideally, if you're going to do just one script, keep all your PHP code at top. Also, other reason for the separation is that IDE editors can easily format HTML nicely. If there's a HTML tag inside the PHP tag that is ending with a HTML tag outside of PHP, then HTML cannot be formatted correctly. For example:

<div><p>And it offers so much <?php echo "$features</p>
<h2>Proven Ideas";?></h2>
<p>More details ahead</p>  
</div>

The above will run just fine but the IDE html formatter will likely be confused with missing end tags and won't format making it more difficult for programmers to read them.

like image 99
netrox Avatar answered Sep 18 '22 11:09

netrox