Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php code inside html or html inside php? [closed]

Tags:

php

I am writing 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>';
?>

And, Which is better in complex programming?

like image 512
Nyasro Avatar asked Feb 17 '13 16:02

Nyasro


2 Answers

First one for sure.
Look, your HTML is

  • natural,
  • can be highlighted,
  • indented
  • checked for syntax errors automatically by the editor.

This is called "PHP template" and being most useful way of separating business logic from presentation logic (save for the special template languages)

like image 162
Your Common Sense Avatar answered Oct 02 '22 09:10

Your Common Sense


The first one is better in all cases. Why? Simply because there is no need to escape any characters, and there is syntax highlighting in editors. It also makes your code more readable (especially if you use indenting in the HTML as well)

Besides, the ability to interleave HTML and PHP is a core feature of PHP. It's meant to be used like this. You can even do stuff like:

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

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

like image 41
Manishearth Avatar answered Oct 02 '22 09:10

Manishearth