带有 HTML 的 PHP 简单 foreach 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10258345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:45:28  来源:igfitidea点击:

PHP simple foreach loop with HTML

phphtmlforeach

提问by sys_debug

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 echoto print the table tags. Will this work? I know in JSP this works.

所以基本上在 foreach 循环中嵌入 HTML 但不使用echo打印表标签。这会起作用吗?我知道在 JSP 中这是有效的。

回答by ilanco

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

尽管在 HTML 中嵌入 PHP 时,最好使用以下形式,但这会起作用:

<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

您可以在此处找到替代语法的文档:http: //www.php.net/manual/en/control-structures.alternative-syntax.php