Question

I was wondering here, why do so many people write their code as

//html
<?php
     //php code
?>
//html
<?php
     //php
?>
//html

does this have some sort of advantage over?

<?php
    echo 'html code';
?>
Was it helpful?

Solution

Both of those approaches are poor for anyone that's not a beginner, or anything outside of a very small standalone script. Beyond a small-sized website or application, a templating engine is normally used to inject PHP data into HTML templates. The syntax of these templates differs by vendor, but typically follows something along the lines of:

<p>Hello {{ name }}</p>

The templating engine is then responsible for loading the HTML, injecting the relevant data from some type of PHP model, and then printing the result, which would of course look something like:

<p>Hello Marty</p>

There are plenty of different engines to chose from, some of which include:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top