mike
mike

Reputation: 1

PHP: generate a number from xyz0000 to xyz9999

I am trying to generate a list of numbers (that I can use in a for/foreach cycle).

Any number should be 10 digits long and have an initial prefix (i.e. 0851)

0851xxxxxx
and go from 0851000000
to
0851999999

Upvotes: 0

Views: 192

Answers (2)

Mchl
Mchl

Reputation: 62395

range('0851000000','0851999999')

However this array will take as much as 84MB in memory (as reported by memory_get_usage(1))

Less memory consuming way is to generate these numbers on the fly, while you iterate in your loop.

For example

for($a = 851000000; $a <= 851999999; $a++) {
  $number = '0'.(string)$a;
  doSomethingWith($number);
}

Upvotes: 7

user142162
user142162

Reputation:

For a random generation, use the following code. Use Mchl's answer if you want the entire list of possibilities.

$str = sprintf("0851%06d", rand(0, 999999));

Upvotes: 3

Related Questions