PHP: How do you determine every Nth iteration of a loop?

后端 未结 8 1339
Happy的楠姐
Happy的楠姐 2020-11-22 13:12

I wanted to echo an image every after 3 post via XML here is my code :



        
8条回答
  •  旧时难觅i
    2020-11-22 13:52

    Use the modulo arithmetic operation found here in the PHP manual.

    e.g.

    $x = 3;
    
    for($i=0; $i<10; $i++)
    {
        if($i % $x == 0)
        {
            // display image
        }
    }
    

    For a more detailed understanding of modulus calculations, click here.

提交回复
热议问题