Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP simple foreach loop with HTML [closed]

Tags:

html

foreach

php

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.

like image 835
sys_debug Avatar asked Apr 21 '12 11:04

sys_debug


1 Answers

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

like image 170
ilanco Avatar answered Oct 17 '22 22:10

ilanco