Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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.
The 4 in %04d sets the total width of the printed value
Add a comment
the number in the first parameter is the "total number of characters" not the number of zeroes to use in padding. What you are looking for is %05d instead.
A more interesting question is why does the following print '0009' instead of '0011'?
<?php $var = 0010; $str = sprintf('%04d', $var+1); echo $str; ?>
So why are you using octal representation anyway? I'm just curious.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.