简体   繁体   中英

Each 3rd Increment Loop PHP

Hi im looking for a way to breakout of a loop every 3 increments to echo a static string.this script echos a "test" after each increment. i want a test after each 3rd increment. any ideas ?

my loop:

<?php

$i = 0; 
while (++$i < 100){
$x = $i - 3;
if ($i+3) {echo $i . "<br>TEST<br>";}
else{   echo $i . "<br>";}



}
?>

try below Code

<?php
   for($i=1;$i<=100;$i++)
   {
       if($i%3==0)
       {
           echo $i."test"."<br>";
       }
   }
?>

use % operator in if loop,

i = 0; 
while (++$i < 100){
$x = $i - 3;
if ($i%3 == 0) {echo $i . "<br>TEST<br>";}
else{   echo $i . "<br>";}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM