47

I'm trying to generate invoice numbers. They should always be 4 numbers long, with leading zeros, for example :

  • 1 -> Invoice 0001
  • 10 -> Invoice 0010
  • 150 -> Invoice 0150

etc.

1

7 Answers 7

100

Use str_pad().

$invID = str_pad($invID, 4, '0', STR_PAD_LEFT);

12
  • 2
    @Dave Using printf functions is an equally valid way to do it here, but since Invoice IDs won't be used as numbers (mathematically speaking), treating them as strings like this is simply another way to do it. If there were ever non-numeric Invoice IDs (e.g., "order 001F3"), then string it is.
    – Wiseguy
    Jun 9, 2011 at 17:31
  • 1
    @Wiseguy, thanks that worked even better than printf because I had to add a prefix to the numbers also, like INV0023.
    – mike23
    Jun 9, 2011 at 17:42
  • 1
    Ack, but strings can also be padded with printf('INV%04s',$something);. Then again: if the invoice if the only fixed width thing you need, this solution is indeed better. If you need more fields on a line to be fixed-width (writing to fixed width files etc.), printf would probably be a better solution.
    – Wrikken
    Jun 9, 2011 at 17:57
  • 1
    @mike23 Ah, I see. printf() actually outputs the string (like print or echo does); sprintf() works identically but returns the string rather than prints it. You'd want sprintf().
    – Wiseguy
    Jun 10, 2011 at 12:47
  • 1
    @mike23 Everybody's new at some point. Glad to see you're learning quickly. :-)
    – Wiseguy
    Jun 10, 2011 at 12:57
30

Use sprintf: http://php.net/function.sprintf

$number = 51;
$number = sprintf('%04d',$number);
print $number;
// outputs 0051


$number = 8051;
$number = sprintf('%04d',$number);
print $number;
// outputs 8051
3
  • perhaps because I explicitly wrote that the strings should be 4 chars long in the end, and you cite two examples with 8 chars.
    – mike23
    Jun 10, 2011 at 6:14
  • 6
    pfft, change the 8 to a 4... I was demonstrating the concept. Jun 10, 2011 at 14:27
  • is sprintf (memory, cpu ) faster then str_pad and thread safe?
    – user285594
    Jul 13, 2016 at 8:26
12

Use (s)printf

printf('%04d',$number);
4

Try this:

$x = 1;
sprintf("%03d",$x);
echo $x;
4

printf() works fine if you are always printing something, but sprintf() gives you more flexibility. If you were to use this function, the $threshold would be 4.

/**
 * Add leading zeros to a number, if necessary
 *
 * @var int $value The number to add leading zeros
 * @var int $threshold Threshold for adding leading zeros (number of digits 
 *                     that will prevent the adding of additional zeros)
 * @return string
 */
function add_leading_zero($value, $threshold = 2) {
    return sprintf('%0' . $threshold . 's', $value);
}

add_leading_zero(1);      // 01
add_leading_zero(5);      // 05
add_leading_zero(100);    // 100
add_leading_zero(1);      // 001
add_leading_zero(5, 3);   // 005
add_leading_zero(100, 3); // 100
add_leading_zero(1, 7);   // 0000001
3

Use the str_pad function

 //pad to left side of the input
$my_val=str_pad($num, 3, '0', STR_PAD_LEFT)

//pad to right side of the input
$my_val=str_pad($num, 3, '0', STR_PAD_RIGHT)

//pad to both side of the input
$my_val=str_pad($num, 3, '0', STR_PAD_BOTH)

where $num is your number

1
1
while ( strlen($invoice_number) < 4 ) $invoice_num = '0' . $invoice_num;
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.