Usually when it comes to invoicing you will need to generate consecutive numbers. For example in Bulgaria the invoice numbers should be 10 digits long. So if you start from your first invoice its number should be 0000000001, then next invoice should be 0000000002, and so on. As you can imagine you cannot just make a cycle to generate these numbers like
<?php
for ($number = 1; $number<10; $number++) {
echo $number;
}
?>
The above code will generate the following numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. You will have to manually add the extra zeros in front of each number. However, there is a very useful PHP function str_pad which will add any string (in our case this is just 0) to the start or end of a string so it becomes a certain length.
We've created a simple function where you can specify start number, count and how many digits the generated numbers should be.
generate_numbers($start, $count, $digits)
$start - is the number for your first invoice
$count - how many invoice numbers you want to generate
$digits - how many digits the generated numbers should be
<?php
function generate_numbers($start, $count, $digits) {
$result = array();
for ($n = $start; $n < $start + $count; $n++) {
$result[] = str_pad($n, $digits, "0", STR_PAD_LEFT);
}
return $result;
}
?>
So if you call the function like this
$numbers = generate_numbers(9992, 20, 10);
it will generate an array $numbers with the following values
Array
(
[0] => 0000009992
[1] => 0000009993
[2] => 0000009994
[3] => 0000009995
[4] => 0000009996
[5] => 0000009997
[6] => 0000009998
[7] => 0000009999
[8] => 0000010000
[9] => 0000010001
[10] => 0000010002
[11] => 0000010003
[12] => 0000010004
[13] => 0000010005
[14] => 0000010006
[15] => 0000010007
[16] => 0000010008
[17] => 0000010009
[18] => 0000010010
[19] => 0000010011
)
As you can see the first eight numbers are 4 digits long (9992 to 9999) and hence we have six 0 added in front of them. Then we have 5 digits long numbers (10000 to 10011) and for these numbers we have five 0 added in front of them.