-2

I have a number like 00001 and I want to keep adding 1 to it till I reach 00044

How can I do this in PHP?

I tried but it takes it as 1 not 00001

So basically what I want is

00001
00002
00003
00004
00005
00006
00007

00008
00009
00010
00011
.
.
.
.
halfer
  • 19,824
  • 17
  • 99
  • 186
user2861783
  • 63
  • 1
  • 6
  • Do your calculations with `1`, `2`, and so on. Those are integers, PHP can calculate with integers. Once you are going to print the variable, apply formatting to get the desired `00001`. That's a string. PHP can print it, but can't calculate with it. (Yes, this is slightly simplified) – domsson Jul 12 '17 at 10:10
  • PHP performs automatic conversions when working with numbers, and will not consider the leading zeros, except when the format will make it consider as octal. You have to work with strings, those numbers with many leading zeros "don't exist" in php. – Kaddath Jul 12 '17 at 10:12
  • `sprintf("%05d", $number);` will print your number and zero-pad it to 5 characters. You can use `$number` however you want (eg. incrementing) before printing/formatting it that way). – ccKep Jul 12 '17 at 10:18
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Jul 12 '17 at 12:26

3 Answers3

1

With str_pad:

For($i=1; $i<=44; $i++){
    Echo str_pad($i,5,"0",STR_PAD_LEFT) ."\n";
}

https://3v4l.org/Pe3M2

Str_pad(start number, length of string you want, what type of padding, where to place the padding [left/right])

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • This doesnot work because if i have a number like 048616 it will make it 48617 instead of 048617 – – user2861783 Jul 12 '17 at 10:34
  • 1
    @user2861783 WHAT???!! Your question is between 1->44 with five digits. Now you come here with a six digit number way above what you asked for?! Correct your question! My answer is not wrong. – Andreas Jul 12 '17 at 10:47
  • @user2861783 if you read the yellow box in my answer you should be able to understand the problem – Andreas Jul 12 '17 at 11:08
  • Thank you this sort of solved the problem but some logic behind the length of string i wanted to get dynamically – user2861783 Jul 14 '17 at 06:06
  • @user2861783 if you want a dynamic lenght then (depending on the lenght) make something like: `Echo str_pad($i, strlen($i)+1, "0",STR_PAD_LEFT) ."\n";` this will make the lenght one more than the number lenght – Andreas Jul 14 '17 at 06:12
0
 for($i=00001;$i<=10;$i++){
     print str_pad($i, 5, 0, STR_PAD_LEFT)."<br>";  
 }

if u want to increment by 1

 $i=00001;
 str_pad($i+1, 5, 0, STR_PAD_LEFT);
Dharmendra Singh
  • 1,186
  • 12
  • 22
  • I have the numbers starting at lets say 00001 not 1 – user2861783 Jul 12 '17 at 10:06
  • PHP will always ignore the leading zeros when the variable is numerical. But it doesn't matter; how you actually print the number is a different matter entirely - a question of formatting, really. – domsson Jul 12 '17 at 10:08
  • 1
    actually on certain conditions, starting with a 0 will have PHP to consider the number as being in octal base – Kaddath Jul 12 '17 at 10:10
  • I tried with 00001 but it wont increment it by 1. I want to keep adding 1 to it so the next time it is 00002 and so on – user2861783 Jul 12 '17 at 10:10
  • This doesnot work because if i have a number like 048616 it will make it 48617 instead of 048617 – user2861783 Jul 12 '17 at 10:34
  • @user2861783 funny how people can ignore meaningful informations. if you have a number like 048616 if won't be understood like you think. actually `$b = 048616; echo $a; echo $a+1;` will echo `4` and `5` – Kaddath Jul 12 '17 at 10:47
0

You should try this: str_pad

<?php

for($n=0;$n<44;$n++){
 $n2 = str_pad($n + 1, 5, 0, STR_PAD_LEFT);
 echo $n2;
 }
?>

http://codepad.org/WqDGMTc1

NID
  • 3,238
  • 1
  • 17
  • 28