Search code examples
phprandom

How to generate a 9 digit number


I'm trying to use php to generate a 9 digit number that does not start with a 0 and is separated by dash every third number.

e.g. 102-394-458

I'm also debating whether

  • to store it as a number or do the formatting on the front end
  • or to store it as a number and do the formatting on the backend
  • or to just store the dashes in the database as well

Of course the choice will affect how the number is generated.


Solution

  • implode('-',str_split(rand(100000000,999999999),3))
    

    Generally, it's probably better simply to store as a number, and format it with the - only for display purposes