如何在php中循环$ num,例如 1,2,3,4,1,2,3,4,1,2,3,4,

I am applying classes to elements on my page using $num in PHP.

So I have:

<?php $num = 0; ?>

<div class="<?php print ++$num; ?>"></div>

I have 16 divs, but I don't want the classes to go all the way to 16. I want them to go up to 4 and then back down to 1. So I want the classes as:

1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4

rather than

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16

Can anyone give me some insight?

Thanks a bunch!

This should solve it.

<div class="<?php echo ($num++ % 4) + 1; ?>"></div>

(edited)

Pure PHP code. Use this logic to fix your need.

<?php 
$num = 0; 
while($num < 16){
    echo ($num++ % 4) + 1; 
}
?>

This gives

1234123412341234

I guess, something like this:

<?php
$num = 0;
while($num<4) {
$num++;
   echo "<div class='".$num."'></div>";
   if ($num==4) {
   $num=0;
}
}

?>