phpformattingprintftruncation

Why is %04d in sprintf() truncating the output to 4 digits?


Here is my code:

<?php
$variable1 = 00001;
$variable2 = (sprintf('%04d', $variable1 + 1));
echo $variable2;
?>

How come the outcome of this code is "0002" and not "00002"? in other words it is missing a 0.


Solution

  • The 4 in %04d sets the total width of the printed value