简体   繁体   English

循环中的endforeach?

[英]endforeach in loops?

I use brackets when using foreach loops. 我在使用foreach循环时使用括号。 What is endforeach for? 什么是endforeach?

It's mainly so you can make start and end statements clearer when creating HTML in loops: 主要是因为在循环中创建HTML时,可以使开始和结束语句更清晰:

<table>
<? while ($record = mysql_fetch_assoc($rs)): ?>
    <? if (!$record['deleted']): ?>
        <tr>
        <? foreach ($display_fields as $field): ?>
            <td><?= $record[$field] ?></td>
        <? endforeach; ?>
        <td>
        <select name="action" onChange="submit">
        <? foreach ($actions as $action): ?>
            <option value="<?= $action ?>"><?= $action ?>
        <? endforeach; ?>
        </td>
        </tr>
    <? else: ?>
         <tr><td colspan="<?= array_count($display_fields) ?>"><i>record <?= $record['id'] ?> has been deleted</i></td></tr>
    <? endif; ?>
<? endwhile; ?>
</table>

versus

<table>
<? while ($record = mysql_fetch_assoc($rs)) { ?>
    <? if (!$record['deleted']) { ?>
        <tr>
        <? foreach ($display_fields as $field) { ?>
            <td><?= $record[$field] ?></td>
        <? } ?>
        <td>
        <select name="action" onChange="submit">
        <? foreach ($actions as $action) { ?>
            <option value="<?= $action ?>"><?= action ?>
        <? } ?>
        </td>
        </tr>
    <? } else { ?>
         <tr><td colspan="<?= array_count($display_fields) ?>"><i>record <?= $record['id'] ?> has been deleted</i></td></tr>
    <? } ?>
<? } ?>
</table>

Hopefully my example is sufficient to demonstrate that once you have several layers of nested loops, and the indenting is thrown off by all the PHP open/close tags and the contained HTML (and maybe you have to indent the HTML a certain way to get your page the way you want), the alternate syntax ( endforeach ) form can make things easier for your brain to parse. 希望我的示例足以证明,一旦你有几层嵌套循环,并且所有PHP打开/关闭标记和包含的HTML都会抛弃缩进(也许你必须以某种方式缩进HTML以获得你的以你想要的方式页面, 替代语法endforeach )表单可以让你的大脑更容易解析。 With the normal style, the closing } can be left on their own and make it hard to tell what they're actually closing. 使用正常的样式,关闭}可以自行保留,并且很难说出它们实际关闭的内容。

It's the end statement for the alternative syntax : 它是替代语法的结束语句:

foreach ($foo as $bar) :
    ...
endforeach;

Useful to make code more readable if you're breaking out of PHP: 如果你突破PHP,有助于使代码更具可读性:

<?php foreach ($foo as $bar) : ?>
    <div ...>
        ...
    </div>
<?php endforeach; ?>

as an alternative syntax you can write foreach loops like so 作为替代语法,您可以像这样编写foreach循环

foreach($arr as $item):
    //do stuff
endforeach;

This type of syntax is typically used when php is being used as a templating language as such 当php被用作模板语言时,通常使用这种类型的语法

<?php foreach($arr as $item):?>
    <!--do stuff -->
<?php endforeach; ?>

How about this? 这个怎么样?

<ul>
<?php while ($items = array_pop($lists)) { ?>
    <ul>
    <?php foreach ($items as $item) { ?>
        <li><?= $item ?></li>
    <?php
    }//foreach
}//while ?>

We can still use the more widely-used braces and, at the same time, increase readability. 我们仍然可以使用更广泛使用的括号,同时提高可读性。

It's just a different syntax. 这只是一种不同的语法。 Instead of 代替

foreach ($a as $v) {
    # ...
}

You could write this: 你可以这样写:

foreach ($a as $v):
    # ...
endforeach;

They will function exactly the same; 它们的功能完全相同; it's just a matter of style. 这只是一种风格问题。 (Personally I have never seen anyone use the second form.) (我个人从未见过有人使用过第二种形式。)

Using foreach: ... endforeach; 使用foreach: ... endforeach; does not only make things readable, it also makes least load for memory as introduced in PHP docs So for big apps, receiving many users this would be the best solution 它不仅使事物可读,而且对PHP内存中的内存负载也最小。因此,对于大型应用程序,接收许多用户这将是最佳解决方案

How about that? 那个怎么样?

<?php
    while($items = array_pop($lists)){
        echo "<ul>";
        foreach($items as $item){
            echo "<li>$item</li>";
        }
        echo "</ul>";
    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM