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

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

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



        
8条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 13:52

    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.

提交回复
热议问题