Modulus in a PHP Loop

Modulus in a PHP loop

Modulus checks what's the leftover of a division.

If $i is 10, 10/2 = 5 with no leftover, so $i modulus 2 would be 0.

If $i is 10, 10/3 = 3 with a leftover of 1, so $i modulus 3 would be 1.

To make it easier for you to track the number of item i would start $i from 1 instead of 0. e.g.

for($i=1; $i <= $count; $i++)
if($i % 2 == 0) echo 'This number is even as it is divisible by 2 with no leftovers! Horray!';

php modulus in a loop by displaying only 5 items per row

You have to do little bit of calculation to find out extra number of divs required. Given the fact that $totalcount is your number of rows, $totalcount would be your necessary number of divs. And subsequently you would have to calculate extra number of divs in the following way,

$totalcount = 7; // or some other value
$totaldivs = $totalcount + (5 - ($totalcount % 5));
$extradivs = $totaldivs - $totalcount; // extra divs

Finally you can use two loops, one printing actaul rows inside divs and other printing extra divs, like this:

// loop printing rows inside divs
for($i = 0; $ < $totalcount; ++$i){
echo '<div>';
// your rows
echo '</div>';
}

// loop printing extra divs
for($i = 0; $ < $extradivs; ++$i){
echo '<div>';
...
echo '</div>';
}

Sidenote: I kept if($key == $len - 1){ ... } block outside of the equation but you can place it in the appropriate place as per your requirement.

How to produce modulus 3 from loop counter in PHP?

You may be after something like:

$counter = 0;
foreach( $items as $item ){

if( $counter <= 1 ) continue;
$class = ( ( ( $counter - 2 ) % 3 ) + 1 );
$counter++;

}

So you would have this class list:

counter         class
------- --------
1 nothing
2 1
3 2
4 3
5 1
6 2
7 3

Using modulus/loop and placing in every 5th position

One Solution:

You can modify your code as like that:

<?php
$my_array = array("Tesla", "BMW", "Ford", "Jeep");
$totalCount = count($my_array); // get the total count of your array
$iteration = $totalCount*5; // multiply as per your iteration

$newArray = array(); // initialize an array
$incArr = 0; // increment for your value's array
for ($i=1; $i <= $iteration; $i++) {
if(($i % 5 == 0) ) { // at every fifth index
$newArray[] = $my_array[$incArr]; // store actual value
$incArr++;
}
else{
$newArray[] = "..."; // store if not a 5th value
}
}
?>

Result:

<?php
$number = 1;
foreach ($newArray as $key => $value) {
echo $number.") ".$value."<br/>";
$number++;
}
?>

Sandbox:

Should be printed as:

1) ...
2) ...
3) ...
4) ...
5) Tesla
6) ...
7) ...
8) ...
9) ...
10) BMW
11) ...
12) ...
13) ...
14) ...
15) Ford
16) ...
17) ...
18) ...
19) ...
20) Jeep

PHP for loop do something different to every second and third item

You're using a switch where you should be using an if. The two are not 100% interchangeable.

In particular, your switch on $i will never be truly equal to the boolean (true/false) conditions in the case clauses. More generally, dynamic case clauses are usually an indication that you should be using an if anyway. switch is meant for checking a dynamic value against constant options, a specific set of known possible values.

Just use an if to clearly express the logic:

for($i = 1; $i <= 20; $i++) {
if ($i % 3 == 0) {
/* Do column three*/
} else if ($i % 2 == 0) {
/* Do column two*/
} else {
/* Do column one*/
}
}

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

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">'; } 

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

The easiest way is to use the modulus division operator.

if ($counter % 3 == 0) {
echo 'image file';
}

How this works:
Modulus division returns the remainder. The remainder is always equal to 0 when you are at an even multiple.

There is one catch: 0 % 3 is equal to 0. This could result in unexpected results if your counter starts at 0.



Related Topics



Leave a reply



Submit