PHP simple foreach loop with HTML

foreach, html, php

Solution

This will work although when embedding PHP in HTML it is better practice to use the following form:

<table>
    <?php foreach($array as $key=>$value): ?>
    <tr>
        <td><?= $key; ?></td>
    </tr>
    <?php endforeach; ?>
</table>

You can find the doc for the alternative syntax on PHP.net

Problem

I am wondering if it will work best to actually write the following for example: ``` <table> <?php foreach($array as $key=>$value){ ?> <tr> <td><?php echo $key; ?></td> </tr> <?php } ?> </table> ``` So basically embedding HTML inside foreach loop but without using `echo` to print the table tags. Will this work? I know in JSP this works.

Original source