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?
First one for sure.
Look, your HTML is
This is called "PHP template" and being most useful way of separating business logic from presentation logic (save for the special template languages)
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With