Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the best practices for PHP syntax when used within HTML? [closed]

Tags:

html

php

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!

like image 665
beingalex Avatar asked Feb 03 '26 03:02

beingalex


1 Answers

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.

like image 136
Franz Avatar answered Feb 04 '26 15:02

Franz