Catching iterations using php modulus in an irregu

2019-07-07 06:55发布

How can I write a modulus that will select the following iterations in sequence?

1, 4, 5, 8, 9, 12, 13 etc (+3+1r)

I'm working within a loop and counting posts (iterations).

So for example, I could catch every third post (1, 4, 7, 10) by:-

if ($i % 3 == 1) { echo 'something here'; } 

But how can I write one that will catch 1, 4, 5, 8, 9, 12, 13?

2条回答
Juvenile、少年°
2楼-- · 2019-07-07 07:17

Sidenote: If you're curious about the closed form formula:

enter image description here

in case you need to pick up a single n-th term from the sequence.

Otherwise I would suggest using a modulus (like suggested by @Sebb) if you need it in your loop.

查看更多
地球回转人心会变
3楼-- · 2019-07-07 07:27

I'm not quite sure about your algorithm, but it seems like you try to get every 3rd and 4th post not (starting at 0). The fitting code would be:

if(($i % 4 == 0 || $i % 4 == 1) && $i != 0) { /* do stuff */ }
查看更多
登录 后发表回答