-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
.
.
.
.
4
  • 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
    Commented Jul 12, 2017 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
    Commented Jul 12, 2017 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).
    – Kep
    Commented Jul 12, 2017 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? - 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
    Commented Jul 12, 2017 at 12:26

3 Answers 3

2

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])

5
  • This doesnot work because if i have a number like 048616 it will make it 48617 instead of 048617 – Commented Jul 12, 2017 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
    Commented Jul 12, 2017 at 10:47
  • @user2861783 if you read the yellow box in my answer you should be able to understand the problem
    – Andreas
    Commented Jul 12, 2017 at 11:08
  • Thank you this sort of solved the problem but some logic behind the length of string i wanted to get dynamically Commented Jul 14, 2017 at 6: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
    Commented Jul 14, 2017 at 6: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);
6
  • I have the numbers starting at lets say 00001 not 1 Commented Jul 12, 2017 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
    Commented Jul 12, 2017 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
    Commented Jul 12, 2017 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 Commented Jul 12, 2017 at 10:10
  • This doesnot work because if i have a number like 048616 it will make it 48617 instead of 048617 Commented Jul 12, 2017 at 10:34
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.