Jump to content

help making number ranges with different steps


coolguydudeman

Recommended Posts

i am trying to make a range of numbers which have a step of 11 then 7 then 11 which can then be compared.  the result im after is 1, 12, 19, 30. Is there a way i could do this without storing the sets in an array and looping though them? Any help would be greatly appreciated, thanks.

Thanks for the reply mjdamato - I apologise for the bad explanation, I would like PHP to automatically create a number sequence starting at 1 and finishing at 30 with a step order of 11, 7, 11 without storing any information in arrays, I was thinking a for loop, but my attempts havent proven sucessful.

Thanks for the reply mjdamato - I apologise for the bad explanation, I would like PHP to automatically create a number sequence starting at 1 and finishing at 30 with a step order of 11, 7, 11 without storing any information in arrays, I was thinking a for loop, but my attempts havent proven sucessful.

 

Hmm... I guess I understand now. But, as Pikachu stated you would obviously need to to something with the values. Anyway, here is a simple loop to do as you ask. I've made it very flexible so you can change the parameters as needed. THis just echos the values to the page, but you can do with it what you want.

//User configurable values
$value = 1;  //The start value
$maxValue = 30; ??The maximum value before the loop will exit
$stepValues = array(11, 7); //The values to be added on each step, you can add more values - will repeat

//Non user configurable value
$step = 0;

//The loop
while($value <= $maxValue)
{
    echo "{$value}<br />\n";
    $value += $stepValues[$step++%count($stepValues)];
}

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.