Why generate simple HTML via PHP heredoc?

I have inherited a website for which I need to make some changes. As always, there are many ways to do something in the development of the site. However, this particular site confused me. All html is delivered via heredoc php scripts. However, no one is handling php.

For example (structure only) index.php:

<?php $html = <<<html <html> <head></head> <body> {more simple HTML here. No php processing to be done} </body> </html> html; echo $html; ?> 

I understand php, but I can’t understand why they generate the page in such a way as to withstand simple delivery without processing the php variable. Before I simply assumed that the person had no idea what they were doing, I thought I would ask. Any ideas?

This is my first post on StackOverflow, so welcome feedback on forum etiquette.

+6
source share
4 answers

Perhaps this is due to the fact that they intended to use PHP variables in HEREDOC. In your example, there are none, but I would venture to suggest that they are used locally. From there, I will simply imagine that HEREDOC is used everywhere for consistency.

+1
source

The reason for heredocs is probably because you do not need to hide quotes or worry about quotes at all in heredoc, whereas string literals you have to manage quotes accordingly.

Most often, keep HTML in a separate file or use a template system - this is usually a better option than heredocs or string literals.

+1
source

PHP files are already template files in HTML.

Replace the code as follows:

 <html> <head></head> <body> {more simple HTML here. No php processing to be done} </body> </html> 

Just having HTML in the body of a PHP file, outside of <?php and ?> , Returns HTML.

+1
source

You could spend several weeks trying to get into the head of an inherited developer, and that’s just wasted time.

I would suggest refactoring the code so that nothing is needed in PHP in this case, there is no use to doing what they did, and every time you go into editing it, it will be a problem for jarring.

+1
source

All Articles