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 0851999999range('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);
}
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));
精彩评论