PHP 循环中的模数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8135404/
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-26 04:02:33  来源:igfitidea点击:

Modulus in a PHP loop

phpmodulus

提问by Udders

I'm currently checking whether an entry in a loop is the third iteration or not, with the following code:

我目前正在使用以下代码检查循环中的条目是否为第三次迭代:

<?php for ($i = 0; $i < count($category_news); $i++) : ?>

    <div class="grid_8">
        <div class="candidate snippet <?php if ($i % 3 == 2) echo "end"; ?>">
            <div class="image shadow_50">
                <img src="<?php echo base_url();?>media/uploads/news/<?php echo  $category_news[$i]['url']; ?>" alt="Image Preview" width="70px" height="70px"/>
            </div>
               <h5><?php echo $category_news[$i]['title']?></h5>
            <p><?php echo strip_tags(word_limiter($category_news[$i]['article'], 15)); ?></p>
            <?php echo anchor('/news/article/id/'.$category_news[$i]['news_id'], '&gt;&gt;', array('class' => 'forward')); ?>
        </div>
    </div>

    <?php if ($i % 3 == 2) : ?>
         </li><li class="row">
    <?php endif; ?>

<?php endfor; ?>

How can I check if the loop is in its second and not its third iteration?

如何检查循环是否处于第二次而不是第三次迭代?

I have tried $i % 2 == 1to no avail.

我试过$i % 2 == 1无济于事。

回答by Shai Mishali

Modulus checks what's the leftover of a division.

模数检查除法的剩余部分。

If $i is 10, 10/2 = 5 with no leftover, so $i modulus 2 would be 0.
If $i is 10, 10/3 = 3 with a leftover of 1, so $i modulus 3 would be 1.

如果 $i 为 10,则 10/2 = 5 且没有剩余,因此 $i 模数 2 将为 0。
如果 $i 为 10,则 10/3 = 3 且剩余值为 1,因此 $i 模数 3 将为 1。

To make it easier for you to track the number of item i would start $i from 1 instead of 0. e.g.

为了让您更容易跟踪项目的数量,我将从 1 而不是 0 开始 $i。例如

for($i=1; $i <= $count; $i++)
    if($i % 2 == 0) echo 'This number is even as it is divisible by 2 with no leftovers! Horray!';

回答by Gustav Bertram

When in doubt, write a snippet of code:

如有疑问,请编写一段代码

for ($j = 1; $j < 4; $j++)
{
   for ($k = 0; $k < $j; $k++)
   {
      echo "\n$i % $j == $k: \n";

      for ($i = 0; $i < 10; $i++)
      {
         echo "$i : ";
         if ($i % $j == $k)
         {
            echo "TRUE";
         }
         echo " \n";
      }
   }
}

Here is the output. Use it to figure out what you need to use:

这是输出。使用它来确定您需要使用什么:

$i % 1 == 0: 
0 : TRUE 
1 : TRUE 
2 : TRUE 
3 : TRUE 
4 : TRUE 
5 : TRUE 
6 : TRUE 
7 : TRUE 
8 : TRUE 
9 : TRUE 

$i % 2 == 0: 
0 : TRUE 
1 :  
2 : TRUE 
3 :  
4 : TRUE 
5 :  
6 : TRUE 
7 :  
8 : TRUE 
9 :  

$i % 2 == 1: 
0 :  
1 : TRUE 
2 :  
3 : TRUE 
4 :  
5 : TRUE 
6 :  
7 : TRUE 
8 :  
9 : TRUE 

$i % 3 == 0: 
0 : TRUE 
1 :  
2 :  
3 : TRUE 
4 :  
5 :  
6 : TRUE 
7 :  
8 :  
9 : TRUE 

$i % 3 == 1: 
0 :  
1 : TRUE 
2 :  
3 :  
4 : TRUE 
5 :  
6 :  
7 : TRUE 
8 :  
9 :  

$i % 3 == 2: 
0 :  
1 :  
2 : TRUE 
3 :  
4 :  
5 : TRUE 
6 :  
7 :  
8 : TRUE 
9 :  

回答by PeeHaa

Now for the answer:

现在回答:

How can I check if the loop is in its second and not its third iteration?

如何检查循环是否处于第二次而不是第三次迭代?

$i % 2 === 0

回答by Gfox

For every third iteration you need:

对于每第三次迭代,您需要:

if ($i % 3 === 0)

If a particular third iteration, then:

如果是特定的第三次迭代,则:

if ($i === 3)

回答by Sudhir Bastakoti

I think it should be:

我觉得应该是:

if ($i % 2 == 0) 

回答by Riz

Try this. It should work for every third iteration:

尝试这个。它应该适用于每三次迭代:

if ($i % 3 === 0)