Jump to content

Probably Simple loop Question


mrras25

Recommended Posts

I just can not wrap my mind around it at this point... I have a couple of for loops and what I need to do is create a numbered sequence that assigns a number to 2 strings after the second one it needs to increment by 1. (since I am speaking in circles here is an example)

 

for($h = 1; $h < 5; $h++) {
   $numb = (100*$h); 
   for($i = 1; $i < 17; $i++) {
       $secNumb = ($numb +20);
       echo "First NUMBER: ".($i +$numb)."Second Number: ".$secNumb."\n"; 
       //The Second number needs to be displayed twice see below for example and this is where i need help
   }
}

 

     First NUMBER: 101 Second Number: 120
     First NUMBER: 102 Second Number: 120
     First NUMBER: 103 Second Number: 121
     First NUMBER: 104 Second Number: 121
     First NUMBER: 105 Second Number: 122
     First NUMBER: 106 Second Number: 122 ...

Link to comment
https://forums.phpfreaks.com/topic/126688-probably-simple-loop-question/
Share on other sites

<?PHP

$numbers = 6;
$second = 120;

for($i = 1; $i <= $numbers; $i++) {

       $first = $i + 100;
       echo "FIRST NUMBER: $first SECOND NUMBER: $second \n"; 
       if ($first % 2 == 0) $second++;

}
?>

 

Produced this output:

 

FIRST NUMBER: 101 SECOND NUMBER: 120
FIRST NUMBER: 102 SECOND NUMBER: 120
FIRST NUMBER: 103 SECOND NUMBER: 121
FIRST NUMBER: 104 SECOND NUMBER: 121
FIRST NUMBER: 105 SECOND NUMBER: 122
FIRST NUMBER: 106 SECOND NUMBER: 122

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.