# Invoice check number

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
I need to create a valid mod10 or luhn number that can be attached to
an invoice, I have found several examples showing how to validate a
credit card number or validate a number using mod10/luhn.

Example:
invoice number = 700123
invoice check number 700123(checknumber)

that check number should make the entire invoice number able to
validate using mod10/luhn

## Re: Invoice check number

I cobbled this together from several sources; will it help?

Validate it thoroughly before relying on it!

<?php
\$inv = "700123";
echo \$inv . checkdigit(\$inv);

function checkdigit(\$num) {
/*
*  1. Reverse the number
*  2. Multiply all the digits in odd positions (The first digit, the third
digit, etc) by 2.
*  3. If any one is greater than 9 subtract 9 from it.
*  4. Sum those numbers up
*  5. Add the even numbered digits (the second, fourth, etc) to the number
you got in the previous step
*  6. The check digit is the amount you need to add to that number to make
a multiple of 10.
*     So if you got 68 in the previous step the check digit would be 2.
*     You can calculate the digit in code using checkdigit = ((sum / 10 +
1) * 10 - sum) % 10
*  -- http://www.darkcoding.net/index.php/credit-card/luhn-formula/
*/
\$sum = 0;
\$pos = 0;
\$rev = strrev(\$num);
\$len = strlen(\$num);
if (\$len % 2 == 0) \$len += 1;
while (\$pos < \$len) {
\$odd = \$rev[\$pos] * 2;
if (\$odd > 9) {
\$odd -= 9;
}
\$sum += \$odd;
if (\$pos != (\$len - 2)) {
\$sum += \$rev[\$pos +1];
}
\$pos += 2;
}
return ((floor(\$sum/10) + 1) * 10 - \$sum) % 10;
}
?>

## Re: Invoice check number

looks like its doing the job, great.

## Re: Invoice check number

Hi

Your example works well but produces two Notices in php.

Nel.
Output follows:
_______________________________________________

Notice: Uninitialized string offset: 6 in luhn.php on line 23

Notice: Uninitialized string offset: 7 in luhn.php on line 29
7001233