How can I increment by a value by 1 for every 50 times a foreach loop runs

You can use another counter to check when you’ve done 50 iterations

<?php

$counter = 1;
$MiniCounter = 0;
foreach ($numbers as $num)
{
    // Pre-increment since $MiniCounter starts by 0
    if (++$MiniCounter >= 50) // using >= 50 because, who knows, $MiniCounter may jump magically from 49 to 51
    {
        $MiniCounter = 0; //reset the mini counter
        $counter++;
    }
}
?>

Browse More Popular Posts

Leave a Comment