1

In php, when I increment:

$numb = "00001";
$numb = $numb +1;

The result is 2.

How do I keep the zeroes and having 00002 ?

Useful link for BASH and for JAVA but not able to think of a php version.

Thank you!

Community
  • 1
  • 1
aPugLife
  • 989
  • 2
  • 14
  • 25

1 Answers1

3

Try this:

$numb = "00001";
$numb = $numb +1;
echo str_pad($numb, 5, '0', STR_PAD_LEFT);

https://3v4l.org/gah5c

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • 1
    Wonderful! I found str_pad somewhere in the documentation and an example but couldn't understand how to work with it! i'll be able to mark this question in some minutes. Thank you! – aPugLife Dec 16 '16 at 12:18
  • In this case only `$numb = 1` would be enough. :) – Sougata Bose Dec 16 '16 at 12:23
  • 1
    @SougataBose I don't know why people always want to have numbers with zeros in front of... Nobody told them that its bad practice to have data like that? If its just for displaying okay... but often they're saving the numbers exactly this way to a DB.. I'll never understand. – Twinfriends Dec 16 '16 at 12:26
  • what if number of zeros is dynamic? – RomanPerekhrest Dec 16 '16 at 12:28
  • @Twinfriends Well.. If you access the database from other ways too. Say a BI software, then having the leading zeros is probably vital. But either way, what the OP asks for, the OP gets. (unless it's way out there) – Andreas Dec 16 '16 at 12:33