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.
Jakegould
source share