Append 0’s in front of a running number

Sometimes we need to fix a number to say 5 digits. e.g. 00012. Here’s the code to append the 0’s in front of the numbers so that you can just save the number without 0’s in front in your database.

1
2
$anum = 123;
echo str_pad($anum, 5, '0', STR_PAD_LEFT);

Result will be displayed as 00123

Leave a Reply

Your email address will not be published. Required fields are marked *