Skip to content

Instantly share code, notes, and snippets.

@sgricci
Created March 3, 2010 04:23
Show Gist options
  • Save sgricci/320310 to your computer and use it in GitHub Desktop.
Save sgricci/320310 to your computer and use it in GitHub Desktop.
Luhn Validation in PHP
<?php
/*
* This function will check a number (Credit Card, IMEI, etc.) versus the luhn
* algorithm. (Both mod 10 and mod5)
*
* More information on the luhn algorigthm: http://en.wikipedia.org/wiki/Luhn_algorithm
*
* From the Wikipedia entry:
* As an illustration, if the account number is 49927398716, it will be validated as follows:
1. Double every second digit, from the rightmost: (1×2) = 2, (8×2) = 16, (3×2) = 6, (2×2) = 4, (9×2) = 18
2. Sum all the individual digits (digits in parentheses are the products from Step 1): 6 + (2) + 7 + (1+6) + 9 + (6) + 7 + (4) + 9 + (1+8) + 4 = 70
3. Take the sum modulo 10: 70 mod 10 = 0; the account number is valid.
*/
function luhn_validate($number, $mod5 = false) {
$parity = strlen($number) % 2;
$total = 0;
// Split each digit into an array
$digits = str_split($number);
foreach($digits as $key => $digit) { // Foreach digit
// for every second digit from the right most, we must multiply * 2
if (($key % 2) == $parity)
$digit = ($digit * 2);
// each digit place is it's own number (11 is really 1 + 1)
if ($digit >= 10) {
// split the digits
$digit_parts = str_split($digit);
// add them together
$digit = $digit_parts[0]+$digit_parts[1];
}
// add them to the total
$total += $digit;
}
return ($total % ($mod5 ? 5 : 10) == 0 ? true : false); // If the mod 10 or mod 5 value is equal to zero (0), then it is valid
}
// To Test:
// var_dump(luhn_validate('49927398716'));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment