0

In PHP, I need to display a big 5 digit, graphical counter from a number fetched from a database. Examples:

  • If number = 1, counter should display 00001
  • If number = 15, counter should display 00015
  • if number = 999, counter should display 00999

What's the easiest way to achieve this?

drake035
  • 3,869
  • 33
  • 97
  • 193

3 Answers3

2

You could use str_pad:

$output = str_pad ($input, 5, '0', STR_PAD_LEFT);
p.s.w.g
  • 141,205
  • 29
  • 278
  • 318
1

The versatile printf or sprintf for the value 1 as 00001:

printf('%05d', 1);
AbraCadaver
  • 77,023
  • 7
  • 60
  • 83
0

printf formats have a zero-padding option:

printf("%05d", $number);
alexis
  • 46,350
  • 14
  • 97
  • 153