aPugLife
aPugLife

Reputation: 1039

How to increment a number with zeros of +1, preserving the 0?

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!

Upvotes: 1

Views: 142

Answers (1)

Andreas
Andreas

Reputation: 23958

Try this:

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

https://3v4l.org/gah5c

Upvotes: 3

Related Questions