PHP:您如何确定循环的每第 N 次迭代?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/936242/
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
PHP: How do you determine every Nth iteration of a loop?
提问by kwek-kwek
I wanted to echo an image every after 3 post via XML here is my code :
我想通过 XML 每隔 3 个帖子回显一个图像,这是我的代码:
<?php
// URL of the XML feed.
$feed = 'test.xml';
// How many items do we want to display?
//$display = 3;
// Check our XML file exists
if(!file_exists($feed)) {
die('The XML file could not be found!');
}
// First, open the XML file.
$xml = simplexml_load_file($feed);
// Set the counter for counting how many items we've displayed.
$counter = 0;
// Start the loop to display each item.
foreach($xml->post as $post) {
echo '
<div style="float:left; width: 180px; margin-top:20px; margin-bottom:10px;">
image file</a> <div class="design-sample-txt">'. $post->author.'</div></div>
';
// Increase the counter by one.
$counter++;
// Check to display all the items we want to.
if($counter >= 3) {
echo 'image file';
}
//if($counter == $display) {
// Yes. End the loop.
// break;
//}
// No. Continue.
}
?>
here is a sample first 3 are correct but now it doesn't loop idgc.ca/web-design-samples-testing.php
这是一个示例,前 3 个是正确的,但现在它不会循环 idgc.ca/web-design-samples-testing.php
回答by Powerlord
The easiest way is to use the modulus division operator.
最简单的方法是使用模数除法运算符。
if ($counter % 3 == 0) {
echo 'image file';
}
How this works: Modulus division returns the remainder. The remainder is always equal to 0 when you are at an even multiple.
这是如何工作的:模数除法返回余数。当您处于偶数倍数时,余数始终等于 0。
There is one catch: 0 % 3is equal to 0. This could result in unexpected results if your counter starts at 0.
有一个问题:0 % 3等于 0。如果您的计数器从 0 开始,这可能会导致意外结果。
回答by Hatrix
Going off of @Powerlord's answer,
离开@Powerlord的回答,
"There is one catch: 0 % 3 is equal to 0. This could result in unexpected results if your counter starts at 0."
“有一个问题:0 % 3 等于 0。如果您的计数器从 0 开始,这可能会导致意外结果。”
You can still start your counter at 0 (arrays, querys), but offset it
你仍然可以从 0(数组、查询)开始你的计数器,但是偏移它
if (($counter + 1) % 3 == 0) {
echo 'image file';
}
回答by Greg B
回答by mateusza
every 3 posts?
每3个帖子?
if($counter % 3 == 0){
echo IMAGE;
}
回答by meme
I am using this a status update to show a "+" character every 1000 iterations, and it seems to be working good.
我使用这个状态更新来每 1000 次迭代显示一个“+”字符,它似乎运行良好。
if ($ucounter % 1000 == 0) { echo '+'; }
回答by Julez
You can also do it without modulus. Just reset your counter when it matches.
您也可以在没有模数的情况下进行。只需在匹配时重置您的计数器。
if($counter == 2) { // matches every 3 iterations
echo 'image-file';
$counter = 0;
}
回答by Ivar
How about: if(($counter % $display) == 0)
怎么样: if(($counter % $display) == 0)
回答by Mohan
It will not work for first position so better solution is :
它不适用于第一位置,因此更好的解决方案是:
if ($counter != 0 && $counter % 3 == 0) {
echo 'image file';
}
Check it by yourself. I have tested it for adding class for every 4th element.
自己检查一下。我已经测试了它为每 4 个元素添加类。

