0

Having some problems with print output like alternative rows 3 : 4 column.

<?php
$i = 1;
while($i=1 < 14){
 if($i <= 3){
   echo $i."<br>";
 } else {
   echo $i."<br>";
 }
 $i++;
}

?>

** I Want Output Like **

1 2 3
4 5 6 7 
8 9 10 
11 12 13 14
1
  • That if clause looks a little "useless" if it prints the same output in the else branch, you could just remove it Commented Mar 1, 2022 at 7:38

2 Answers 2

3

Use the modulo (remainder of a division). Apply a modulo 7 on $i:

for ($i=1; $i<15; $i++) {
    echo $i, in_array($i % 7, [0, 3]) ? '<br>' : ' ';
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you just want to output:

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

you can just try this,

$firstRow = 0;
$secondRow = 0;
for ($i = 1; $i <= 14; $i++){
   $firstRow++; 
   $secondRow++;

   echo $i . ' ';

   if($firstRow == 3){
      echo '<br>';
      $secondRow = 0;
   }
   if($secondRow == 4){
      echo '<br>';
      $firstRow = 0;
      $secondRow = 0;
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.