3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $count = 0; $count_total = 10; $columns = 3; $all_posts = array(1,2,3,4,5,6,7,8,9,10); foreach ($all_posts as $k => $v) { $count++; $is_first_column = false; if ($count == 1 || ($count-1) % $columns == 0) { $is_first_column = true; } echo '<div id="post-' . $k . '" class="post-col ' . ($is_first_column ? 'column_1' : '') . '">' . $v . '</div>'; }
Output for 4.3.0 - 7.1.0
<div id="post-0" class="post-col column_1">1</div><div id="post-1" class="post-col ">2</div><div id="post-2" class="post-col ">3</div><div id="post-3" class="post-col column_1">4</div><div id="post-4" class="post-col ">5</div><div id="post-5" class="post-col ">6</div><div id="post-6" class="post-col column_1">7</div><div id="post-7" class="post-col ">8</div><div id="post-8" class="post-col ">9</div><div id="post-9" class="post-col column_1">10</div>