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.
I have an echo = "000000" and a string named $id which is an integer.
If $id is = 1, how do I add "000000" + $id to get 000001 ?
You could check out str_pad
In your case it would be something like:
str_pad('1', 6, '0', STR_PAD_LEFT);
Add a comment
function padWithZeros($s, $n) { return sprintf("%0" . $n . "d", $s); }
Where $n is the number of zeros you want, e.g:
echo padWithZeros("1", 6);
That pads the number 1 with five zeros to equal six places
printf("%06d", $id)
$temp = "00000".$id; echo "ID = $temp";
str_pad is the PHP function that does exactly what you want. However, printf is a well understood method of doing that that works across most common languages. It probably makes sense to use printf in this case to make the code more readable.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.