PHP Maths Logic

I am trying to set a variable based on some math logic (for wrapping a specific html around elements).

I made half the problem to press 0, 3, 6, 9, 12

if(($i % 3) == 0) { // blah } 

Now I need to press the following numbers: 2, 5, 8, 11, 14, etc.

What possible math work can I do to get into this sequence?

+6
math php
source share
3 answers
 if($i % 3 == 1) if($i % 3 == 2) 

Modulo returns the remainder, so when you match 0, you get the 3rd, 6th, 9th, etc., since 0 remains in division.

So just check when 1 is left and 2 is left.

+7
source share

Along with Tor Valamo's answer, you may notice a pattern (3 * $i) - 1

 (3*1)-1 = 2 (3*2)-1 = 5 (3*3)-1 = 8 ... 
+3
source share

if ((($ i-2)% 3) == 0) {// blah}

+2
source share

All Articles