0

If the number is smaller than 10-digit number then it should to add automatically '0', so that the number get to fill to 10-digit number. Example: $number = "123456"; Then add '0' -> it should to show: 0000123456

If example 7-digit number then so: 0001234567

How I code it? Thank you for helping.

mangosaft
  • 31
  • 1
  • 7

1 Answers1

1

str_pad($input, 10, "0", STR_PAD_LEFT);

string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] )

This functions returns the input string padded on the left, the right, or both sides to the specified padding length. If the optional argument pad_string is not supplied, the input is padded with spaces, otherwise it is padded with characters from pad_string up to the limit.

You can also use sprintf to return a formatted string.

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • Thank you for this code snippet, which may provide some immediate help. A proper explanation [would greatly improve](https://meta.stackexchange.com/q/114762) its educational value by showing why this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. – GrumpyCrouton Dec 04 '17 at 17:59
  • You can also do a `sprintf("%010d",$oldnum);` as well. – Forbs Dec 04 '17 at 18:00