modulus operator to run 1st and then every 3rd item

空扰寡人 提交于 2019-12-01 17:18:49

问题


So i need it to run on the first loop and then every 3rd loop

if ($k % 3 || $k==1 ) { echo '<div class="modcontainer">'; } 

Seems simple to me, but i don't have the understanding of modulus


回答1:


Modulus returns the remainder, not a boolean value.

This code will resolve to true for 1, 3, 6, 9, ...

if (($k % 3 == 0) || $k==1 ) { echo '<div class="modcontainer">'; } 

This code will resolve to true for 1, 4, 7, 10, ...

if ($k % 3 == 1) { echo '<div class="modcontainer">'; } 


来源:https://stackoverflow.com/questions/11261192/modulus-operator-to-run-1st-and-then-every-3rd-item

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!