如何将三个div的倍数放入列表项?

I have content that is being outputting in endless divs. I'd like to surround mutiples of three within a list item, like this:

<li>
   <div>content</div>
   <div>content</div>
   <div>content</div>
</li>
<li>
   <div>content</div>
   <div>content</div>
   <div>content</div>
</li>

I'm trying to achieve this with the following code, but it's not working for some reason - it simply surrounds each div in its own li:

$i = 1;
if($setting) {
    echo '<li>';
    echo '<div>My div</div>';
    if ($i % 3 == 0) { 
        echo '</li><li>'; 
        }
    $i++;
    }
    echo '</li>';
} 

Without your loop, hard to say what you're doing wrong.

But here's a solution.

$numOfDivs = 12;

for ($thisDiv=0; $thisDiv < $numOfDivs; $thisDiv++) {
    if ($thisDiv==0) echo '<li>';
    else if ($thisDiv%3==0) echo '</li><li>';
    echo '<div>My Div</div>';
    if ($thisDiv == $numOfDivs-1) echo '</li>';
}

Bear in mind this produces the desired output, but that the desired output is invalid HTML.