bre
bre

Reputation: 25

PHP range issues with number starting with 0

$ids = range(000,010);

gives same results as

$ids = range(0,10);

In short it avoids zeros in the beginning. I want to include the zero for my requirements. I would like to use this function to range from 000-999, how can I do that ?

Upvotes: 0

Views: 94

Answers (1)

Markus Zeller
Markus Zeller

Reputation: 9145

You can format the output.

$ids = array_map(fn($value) => sprintf('%03d', $value), range(0,10));
print_r($ids);

Output

Array
(
    [0] => 000
    [1] => 001
    [2] => 002
    [3] => 003
    [4] => 004
    [5] => 005
    [6] => 006
    [7] => 007
    [8] => 008
    [9] => 009
    [10] => 010
)

Upvotes: 1

Related Questions