-
want to generate 5 digit serial number in php
about 9 years ago
-
about 9 years ago
Hi ,
This will help you to generate a series of serial numbers till 100. I hope it helps you out.
for($i=0;$i<=100;$i++) { echo sprintf("%05d", $i) . "<br>"; }
Thanks
-
-
about 9 years ago
As you mention you need a number which starts from 000 like but a integer variable always ignore the number starts from 0, so first thing you need to hold it as a string may be. Generating incremented number is not a big thing you can use database table or a static integer variable which will give you a number +1 to its previous value.
Now suppose you have a table handling the incremented integer value, which will give you 1 then 2 then 3 and so on whenever you call that function or you can create sequence in db.
int $numericId = getIdFromDB(); // this will give you previous id +1
Suppose you have 1 in variable $numericId. Now create a function getFiveDigitId( $numericId) which will return the string of five digit:
function getFiveDigitId( $numericId){ $id = $numericId/100000; // will give you 0.00001 $strId = strVal($id); // convert to string if(startsWith($strId, "0.")){ return substr($strId, 1); }else{ return $numericId; } }
-
2 Answer(s)