Skip to content

Instantly share code, notes, and snippets.

@anoxic
Created January 15, 2015 12:40
Show Gist options
  • Save anoxic/38344f07d39115bfda3f to your computer and use it in GitHub Desktop.
Save anoxic/38344f07d39115bfda3f to your computer and use it in GitHub Desktop.
<?php
function luhn2($number) {
static $sum_table = [[0,1,2,3,4,5,6,7,8,9],[0,2,4,6,8,1,3,5,7,9]];
$sum = 0;
$number = (string)$number;
$i = strlen($number);
while (--$i) {
$sum += $sum_table[($i+1) % 2][$number[$i]];
}
return $number[strlen($number)-1] == substr($sum*9,-1,1);
}
function luhn($number) {
static $sum_table = [[0,1,2,3,4,5,6,7,8,9],[0,2,4,6,8,1,3,5,7,9]];
$sum = 0;
$number = (string)$number;
$i = strlen($number);
$flip = 0;
while (--$i)
$sum += $sum_table[++$flip % 2][$number[$i]];
return $number[strlen($number)-1] == substr($sum*9,-1,1);
}
var_export(luhn("79927398713"));
var_export(luhn2("79927398713"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment