Some PHP code I look at is littered with "<?php" and "?>" tags depending on whether it's outputting HTML or not. Is there any performance benefit to this rather than using echo to write the HTML? It makes the code extremely hard to read when the code's constantly switching between code and HTML via the "<?php" tag.
Note that I'm not just talking about the occasional switchover. The code I'm currently looking at (the mantis-bt source code) is giving me a headache with the number of times it's switching. Very very hard to read.
I wonder if there's a reason for them doing it like this?
As far as readability goes, I would rather see:
<ul>
<?php foreach ($items as $item): ?>
<li>
<a href="<?php esc($item->url)?>">
<img src="<?php esc($item->icon)?>"/>
<?php esc($item->text)?>
</li>
<?php endforeach; ?>
</ul>
Than:
echo "<ul>";
foreach ($items as $item)
{
echo "<li>";
echo '<a href="'.esc($item->url).'">';
echo '<img src="'.esc($item->icon).'"/>';
echo esc($item->text);
echo '</li>';
}
echo "</ul>";
Not only that, but the latter lets your IDE of choice handle the HTML syntax and formatting (telling you, for instance, that the </a>
is missing). So, unless there's a lot more going on in-between the short bits of HTML, <?php
might be preferable.
EDIT: as for performance, anyone serious about code speed will activate a caching pre-compiler which will boil down both versions to the exact same thing.