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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With