PHP的简单的foreach循环与HTML [英] PHP simple foreach loop with HTML

查看:118
本文介绍了PHP的简单的foreach循环与HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < table> 
<?php foreach($ array as $ key => $ value){?>
< tr>
< td><?php echo $ key; ?>< / TD>
< / tr>
<?php}?>
< / table>因此,基本上嵌入在foreach循环中的HTML,但不使用 echo


$> $ c>打印表格标签。这会工作吗?我知道在JSP中这是有效的。

解决方案

虽然在HTML中嵌入PHP时最好使用下面的表单:

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

您可以在这里找到替代语法的文档: http://www.php.net/manual/en/control-structures.alternative-syntax.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.

解决方案

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><?php echo $key; ?></td>
    </tr>
    <?php endforeach; ?>
</table>

You can find the doc for the alternative syntax here : http://www.php.net/manual/en/control-structures.alternative-syntax.php

这篇关于PHP的简单的foreach循环与HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆