Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 511
Jamie Hutber Avatar asked Jun 29 '12 11:06

Jamie Hutber


People also ask

What does the modulus (%) operator do?

The modulo operator, denoted by %, is an arithmetic operator. The modulo division operator produces the remainder of an integer division.

How do you write a modulus operator?

The modulo operation (abbreviated “mod”, or “%” in many programming languages) is the remainder when dividing. For example, “5 mod 3 = 2” which means 2 is the remainder when you divide 5 by 3.

What is modulus operator with an example?

The modulus operator (also informally known as the remainder operator) is an operator that returns the remainder after doing an integer division. For example, 7 / 4 = 1 remainder 3. Therefore, 7 % 4 = 3. As another example, 25 / 7 = 3 remainder 4, thus 25 % 7 = 4.

What is modulus remainder?

The Modulus is the remainder of the euclidean division of one number by another. % is called the modulo operation. For instance, 9 divided by 4 equals 2 but it remains 1 . Here, 9 / 4 = 2 and 9 % 4 = 1 .


1 Answers

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">'; } 
like image 111
user247702 Avatar answered Oct 12 '22 06:10

user247702