StuBlackett
StuBlackett

Reputation: 3857

Integer value ignoring zeros and returning full number

I'm looking to create a PHP Range loop

In a first and second number in the range, but have noticed the first number which is

00006

Is being rounded down / flattened to show "6".

So when I echo the first number value I get "6" back. I need it to be "00006"

Then the next number will need to be 00007 and so on, via the range loop.

My PHP code at present is :

$first_number = 00006;
$last_number = 11807;

foreach(range($first_number, $last_number) as $id)
{
  echo $id;
}

How do I go about making sure that the number has the previous 0's in it?

Upvotes: 0

Views: 138

Answers (1)

Anisur Rahman
Anisur Rahman

Reputation: 660

You can do it by using printf() function. See the documentation : printf

First of all in PHP, a number starting with zero is treated as octal number, But I guess range() function converts it as decimal. So if you want to start it with 20. Like $first_number = 00020; then the output will be start with 16 not 20.
So, if you want the output starting with 0's, then you can do like this:

$first_number = 6;
$last_number = 11807;

foreach(range($first_number, $last_number) as $id)
{
  printf("%05d",$id);
}

Upvotes: 1

Related Questions