php endforeach 在循环中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4600419/
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
endforeach in loops?
提问by CyberJunkie
I use brackets when using foreach loops. What is endforeach for?
我在使用 foreach 循环时使用括号。endforeach 有什么用?
回答by Brad Mace
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. With the normal style, the closing }
can be left on their own and make it hard to tell what they're actually closing.
希望我的示例足以证明,一旦您有多层嵌套循环,并且所有 PHP 打开/关闭标签和包含的 HTML 都会抛出缩进(也许您必须以某种方式缩进 HTML 才能获得您的按您想要的方式翻页),替代语法( endforeach
) 形式可以让您的大脑更容易解析。使用正常样式,关闭}
可以自行保留,并且很难分辨它们实际关闭的内容。
回答by deceze
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; ?>
回答by acqu13sce
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; ?>
回答by cdhowie
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.)
它们的功能完全相同;这只是风格问题。(就我个人而言,我从未见过有人使用第二种形式。)
回答by Preecha
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.
我们仍然可以使用更广泛使用的大括号,同时增加可读性。
回答by Omar El Don
Using 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
使用foreach:
...endforeach;
不仅使事情可读,而且还使内存负载最小,如 PHP 文档中介绍的那样 因此对于大型应用程序,接收大量用户这将是最好的解决方案
回答by Ressujsiesam
How about that?
那个怎么样?
<?php
while($items = array_pop($lists)){
echo "<ul>";
foreach($items as $item){
echo "<li>$item</li>";
}
echo "</ul>";
}
?>