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,955
  • 41
  • 119
  • 229

3 Answers3

2

You could use str_pad:

$output = str_pad ($input, 5, '0', STR_PAD_LEFT);
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
1

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

printf('%05d', 1);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

printf formats have a zero-padding option:

printf("%05d", $number);
alexis
  • 48,685
  • 16
  • 101
  • 161