PHP Foreach Loop Iterations
<p>I am trying to output a wrapper html tag ONLY when a condition is met but I am having a heard time grasping how modulus work in this context.</p>

<p>So here is what I want when the page is generated.</p>

<pre><code>&lt;div class="container"&gt;
&lt;span class="item"&gt;&lt;/span&gt;
&lt;span class="item"&gt;&lt;/span&gt;
&lt;span class="item"&gt;&lt;/span&gt;
&lt;span class="item"&gt;&lt;/span&gt;
&lt;/div&gt;&lt;!--close container--&gt;

&lt;div class="container"&gt;
&lt;span class="item"&gt;&lt;/span&gt;
&lt;span class="item"&gt;&lt;/span&gt;
&lt;span class="item"&gt;&lt;/span&gt;
&lt;span class="item"&gt;&lt;/span&gt;
&lt;/div&gt;&lt;!--close container--&gt;
</code></pre>

<p>Here is the PHP code where I am trying to achieve this:</p>

<pre><code>    $i = 1;

    foreach( $items as $item ) {
         if ( $i === 1 )
              echo '&lt;div class="container"&gt;';

         echo '&lt;span class="item"&gt;';

         if ( 4 % $i === 0 ) {
              echo '&lt;/div&gt;&lt;!--close container--&gt;';
              echo '&lt;div class="container"&gt;';
         }
         $i++
    }
</code></pre>

<p>But this is not working as should; it outputs first wrap, and then second under wrapped..etc. So I am not even sure if I am suppose to be using modulus and if that is the best approach to this.</p>

<p>Thanks for looking.</p>
