learn4life
learn4life

Reputation: 77

How to make sequential number and repeat it

I would like to make a number sequential and repetitive to the beginning when it reaches the limit. For instance, the first number begins with 001, then 002, 003, and so on until it reaches 999. When it reaches 999, then the number will return to 01 and continues to repeat.

I currently have this loop:

$i = 001;
for ( $i; $i < 111; $i++)
{
echo str_pad ($i, 3, '0', STR_PAD_LEFT), '<br>';
}

But how can I make that return to the first number and continues like before? I mean if the number reaches 999, it will back to 001, 002, 003, and so on.

Upvotes: 0

Views: 103

Answers (1)

Micha&#235;l Garrez
Micha&#235;l Garrez

Reputation: 408

Even if I don't quite understand why you want to do that, this is your solution :

<?php

for ($i = 001, $max = 111; $i < $max; $i++)
{
    echo str_pad ($i, 3, '0', STR_PAD_LEFT), '<br>';

    if ($i == $max - 1) { 
        $i = 001; 
    }
}

But this will cause an infinite loop of course.

Upvotes: 1

Related Questions