PHP code inside html or html inside php?

I write code in php, but I am confused in the following coding styles:

<html> <head></head> <body> <?php echo 'Hello World'; ?> <div>This is another</div> <?php echo 'hello again'; ?> </body> </html> 

OR

 <?php echo '<html> <head></head> <body> Hello World <div>This is another</div> hello again </body> </html>'; ?> 

What is better in complex programming?

+6
source share
2 answers

The first one for sure.
Look, your HTML

  • natural,
  • can be distinguished
  • indentation
  • automatically checks for syntax errors in the editor.

This is called a "PHP template" and is the most useful way of branching business logic from presentation logic (except for special template languages)

+11
source

The first is better in all cases. What for? Just because there is no need to hide any characters, and the editors have syntax highlighting. It also makes your code more readable (especially if you use indentation in HTML)

In addition, the ability to alternate HTML and PHP is a major feature of PHP. It should have been used like that. You can even do things like:

 The programmer says <?php if(isGoodbye()){ ?> goodbye <?php }else{ ?> hello <?php }?> world 

It makes life easy for you, so why not use it?

+7
source

All Articles