I was wondering what the best practice is when using PHP within HTML? Would you use short tags for echo? Would you use curly brackets on if/else statements etc?
Please no comments on using a templating system or that I should be using MVC. I am working on a project that I have been asked to update and ammend.
Here is a block of code that I have written:
<div class="left">
<ul>
<?php foreach ($data['vars']['groupslist'][1] as $key => $group): ?>
<li><?php echo $group->title; ?></li>
<?php endforeach; ?>
</ul>
</div>
Notice I have used : and endforeach;. What would you do?
Thanks!
In PHP templates, that’s exactly the way I do it, too (
foreach/endforeach).But really, when it comes to syntax, this is simply a matter of preference. Thus, no “best practice”. Go with what you like and – more importantly – stick to it!
I’m sure there are good reasons for either approach.