phpvariablesstring-formattingzero-pad

In PHP, how do I add to a zero-padded numeric string and preserve the zero padding?


If I have a variable in PHP containing 0001 and I add 1 to it, the result is 2 instead of 0002.

How do I solve this problem?


Solution

  • $foo = sprintf('%04d', $foo + 1);