Skip to content

Instantly share code, notes, and snippets.

@stackoverflows
Last active June 22, 2018 10:13
Show Gist options
  • Save stackoverflows/f60bdfe34123ca3e187007732204f017 to your computer and use it in GitHub Desktop.
Save stackoverflows/f60bdfe34123ca3e187007732204f017 to your computer and use it in GitHub Desktop.
Working PHP Credit Card luhn check (preliminary credit card number validation)
// will update with suggestions
function luhnCheck($number): bool
{
$number = preg_replace("/[^0-9]/", "", strval($number)); // convert to string and remove non numeric characters
$sum = "";
$revNumber = strrev($number);
$len = strlen($number);
for ($i = 0; $i < $len; $i++) {
$sum .= $i & 1 ? $revNumber[$i] * 2 : $revNumber[$i];
}
return array_sum(str_split($sum)) % 10 === 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment