Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between php inside html and vice versa

Tags:

html

php

I was always curious, is there any significant advantage or disadvantage of writing php inside html or vice versa

example:

echo '<ul>'
foreach ($items as $item)
{
    echo "<li>$item</li>";
}
echo '</ul>

As opposed to:

<ul>
<? foreach($items as $item): ?>
    <li>$item</li>
<? endforeach; ?>
</ul>

Since these essentially generate the same thing, when would you actually use one over the other?

like image 781
clifford.duke Avatar asked Feb 19 '23 15:02

clifford.duke


1 Answers

Functionally they are the exact same and won't have an appreciable affect on performance, if any. It comes down to personal preference and readability - if one is clearer than the other and will be easier for others (or the future you) to understand, go with that one.

like image 155
doublesharp Avatar answered Feb 21 '23 03:02

doublesharp