Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding variable number of zeroes to make a six digit number

Tags:

php

prefix

I am working with a membership database which records all accounts in a six digit format however, some users will be using a sub-six digit format due to older conventions. So I want to be able to accept a sub-six digit number and prefix zeroes to the beginning of it.

e.g. user enters number 1234, I want PHP to format it to become 001234. user enters number 123, I want PHP to format it to become 000123.

like image 383
thisisready Avatar asked Dec 12 '25 06:12

thisisready


2 Answers

You can use PHPs sprintf()-function:

$formattedNumber = sprintf('%06d', $unformattedNumber);
like image 187
jwueller Avatar answered Dec 13 '25 19:12

jwueller


Too Easy

str_pad($input, 6, "0", STR_PAD_LEFT);
like image 22
Randy the Dev Avatar answered Dec 13 '25 20:12

Randy the Dev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!