Jump to content

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)];
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.