How to generate a 9 digit number

I am trying to use php to generate a 9 digit number that does not start with 0, and split the dash into every third number.

eg. 102-394-458

I also discuss

  • to save it as a number or do formatting on the front side
  • or save it as a number and do formatting on the backend
  • or just save the dash in the database as well

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

+3
source share
4 answers
implode('-',str_split(rand(100000000,999999999),3))

Generally, it’s better to just store as a number and format it just for display purposes

+16
source

number_format() -. , "" , - , , .

:

$number = mt_rand(100000000,999999999);

number_format :

echo number_format($number,0,"","-");
+5

1 9. 8 0 9. , , .

+3

( , , .)

,

mt_rand(100000000, 999999999);

:

wordwrap($number, 3, '-', true);
+3

Source: https://habr.com/ru/post/1757497/


All Articles