-1

i need help to generate a sequence numbers in the for loop i have code but it is not working properly as i want to do it is generating random numbers?

if for loop reached to the 999 then automatically how to addition after for loop reached to the 999 for loop should be addition like this 999+1=1000+1=10001?

i want script generate number like this

500,501,502,503,....so on till 999

here my code

$numbers = range(500, 999);
foreach ($numbers as $i) {
echo $say = str_pad($i, 5, "0",  STR_PAD_LEFT).'<br>';
}
16
  • 2
    I think you mean range(500, 999)...
    – elclanrs
    May 7, 2013 at 8:56
  • 1
    And if you want a set display size with leading zeroes, use str_pad() or sprintf() rather than that convoluted series of if statements
    – Mark Baker
    May 7, 2013 at 8:57
  • 1
    Please rephrase your question. You are talking about random numbers at some point, but don't tell us what role those should play in what you are doing. And why should the for loop start with 500 again when it reached 999? That would make it infinite.
    – Till Helge
    May 7, 2013 at 9:00
  • 1
    When you create random numbers you will never "reach" 999. Using rand() doesn't mean that a number can be picked only once. It might happen that several times in a row 999 is picked and sometimes 999 will not be picked at all for a long time. Maybe it would be better if you would explain what you want to do.
    – Till Helge
    May 7, 2013 at 9:13
  • 1
    Why do you use range(500,999) if you want it to continue beyond 999? Just put the number you want as last number in there.
    – Till Helge
    May 7, 2013 at 9:30

3 Answers 3

3

Use a for loop with your range. And use str_pad()

for ($i = 500; $i < 1000; $i++) {
     $say = str_pad($i, 5, "0", STR_PAD_LEFT);
}

And with range():

$numbers = range(500, 999);
foreach ($numbers as $i) {
     $say = str_pad($i, 5, "0", STR_PAD_LEFT);
}

Edit: It is not clear to me how and why you want to start at 500 again when reaching 999, but you could wrap the above in:

while (true) {
    // One of the above solutions
}

WARNING: the above will create an infinit loop!

Try something like this:

$h = 0;
while ($h < 10) {
    // One of the above solutions
    $h++
}

or

for ($h = 0; $h < 10; $h++) {
    // One of the above solutions
}
2
  • it is working now but how do i automatically start with 500 if loop reached to the 999? May 7, 2013 at 9:01
  • It is not clear to me how and why you want to start at 500 again when reaching 999, so I provided some basic solutions of which you should pick the one closest to your needs...
    – 7ochem
    May 7, 2013 at 12:18
0

Try This:

$num=rand(500, 999);
for ($i = 500; $i <=$num ; $i++) {
    echo str_pad($i,5,'0', STR_PAD_LEFT);
}
1
  • how do i automatically start with 500 if loop reached to the 999? May 7, 2013 at 9:04
0

I think this is what you're looking for:

    $seed=rand(500, 999);

    for ($i = 0; $i <500 ; $i++)
    {           
       $num=500+($seed+$i)%500;
       $say = str_pad($num,5,'0', STR_PAD_LEFT);
    }

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.