Why should we separate PHP from HTML?

I am new to programming and I know how to separate PHP from HTML, but I would like to know if there is a difference in work

<?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 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 a difference between performance, etc., between splitting PHP code into HTML code and just echoing the whole page in php?

+7
html php
source share
6 answers

The best practice is not to separate PHP from HTML, it is best to use separate markup logic .

Coding style is also important. Correct indentation of lines . Using echo "</div>"; instead of echo"</div>"; Valid HTML without putting variables in quotes :

 echo "The variable contains the string $rand"; 

better (why? see my comment below):

 echo "The variable contains the string ", $rand, " :-)"; 

Your whole project gains a lot of quality and dignity, simply improving the code, writing clean, readable, supported. Imagine that you want to change text, you will need to add or change a lot of echo es.

Code Style Guides> Pear , PSR , Zend <

Encourage developers to keep their code readable, valid, and compatible with multiple browsers.

+8
source share

The problem is not in performance, but in readability and, more importantly, maintainability .

Performing all the processing in one place and all the output in another (i.e., Logical and Presentation ) means that it will be easier for you to change the time without changing too much affecting another.

For your specific question, the top method is preferable, for the reasons mentioned above.

+5
source share

Taking your question at face value, two reasons immediately arise:

  • Assuming you're using a smart editor, repeating all of your HTML will cause you to lose syntax highlighting, so you are unlikely to catch errors.
  • Since everything is inside a PHP string, now you need to worry about avoiding all the other special characters. Try to spit out some Javascript with a line in it and let us know how fun it is.

However, when most people say something like β€œseparating PHP from HTML”, they refer to the concept of separating your logic from your views. This means that you do not put complex business logic, calculations and database calls inside your html pages. Keep everything in pure PHP files, and your html files contain minimal PHP, which is used only for splashing your data.

+1
source share

I think that you are not a very good example, which very clearly explains why you should share it.

The reason you should separate not only HTML, but also part of the presentation, rendering, or user interface of your application is pure coding and separation of problems . This will allow you to get clean, easy-to-read code and make your application core.

Take Wordpress, for example, this is an extremely ugly combination of php and HTML. They even run SQL queries at the presentation level of the application, if you can even draw the line between the presentation and the other logic in this article.

You always need to output some dynamic content to your HTML, but actually try to reduce it to repeat the variables and to have some auxiliary output formatting objects. All business logic should be somewhere else, and not in "templates" or in any other place that you will call output files.

Look at the MVC pattern , for example, it gives you an idea of ​​how and why you want to separate things.

0
source share

In my opinion, it depends on the level of HTML formatting, which is performed in comparison with the PHP logic. No more no less. It is simply easier to read pure HTML as pure HTML or PHP as direct PHP. When all this shakes together - how some template systems are processed - it becomes a logical headache for reading and debugging. Therefore, I am mistaken on the side of placing HTML in PHP for my own pleasure.

It is not clear how effective the pros or cons, if any. But I can assure you that after 20 years I never had a server slowing down due to too much HTML embedded in PHP

Personally, I would format your sample code as follows:

 <?php echo "<h1>This is a title</h1>" . "<div>" . "<p>This is a paragraph</p>" . "The variable contains the string $rand" . "</div>" ; ?> 

I like this method because there is one echo that makes it clear what is going on, and the rest of the HTML is simply concatenated using characters . .

Also, remember that all formatting in HUMANS software benefits is more than anything else. The computer should only see commands, so if you want to be perfect for the machine, just code without spaces or formatting. Heck, stop using full words and just use 1 variable letters! Oh wait, that's how it was done in ancient times.

Currently, compilers and caching systems are designed to read human-readable code and optimize its operation.

That's all you need to say: you have to code the readability and logic on your part. Nothing more and nothing less.

0
source share
 <?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 split. This is what the php / html split 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> 

In terms of performance, this is not a problem, but it will be very useful for programmers so that they can easily read the code, therefore, the need for HTML / PHP separation methods. Ideally, if you are going to make only one script, keep all your PHP code on top. In addition, another reason for the separation is that IDE editors can easily format HTML. If the PHP tag has an HTML tag that ends with an HTML tag outside of PHP, then the 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 work just fine, but the html IDE formatter is likely to be confused with missing tags and will not format, making it difficult for programmers to read.

0
source share

All Articles