Home  >  Article  >  Backend Development  >  PHP implementation of credit card check digit algorithm THE LUHN MOD-10 example_PHP tutorial

PHP implementation of credit card check digit algorithm THE LUHN MOD-10 example_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:30:17965browse

According to the payment card check digit algorithm in ISO 2894 The Luhn Mod-10 Method stipulates:

1. Multiply each digit on the card number by the weight. The rule is that if the number of digits in the card number is an even number, the first digit is multiplied by 2, otherwise it is multiplied by 1, and then they are, 1, 2, 1, 2, 1, 2;
2. If every If the number multiplied by the weight exceeds 9, you need to subtract 9;
3. Sum all the processed weighted numbers and use the number 10 to calculate the modulo operation;
4. The remainder should be 0, otherwise it may It's a typo. It could also be a fake number.
With the simple implementation of PHP, front-end verification in actual scenarios is better, such as JS.

Copy code The code is as follows:

function check_card($card){
if (!is_numeric($card) ) return False;
$card_len = strlen($card);
$i = 0;
$num_i = array();
do{
if (!$i){
$num_x = $card_len % 2 ? 1 : 2;
} else {
$num_x == 1 ? 2 : 1;
}
       $num_i[$i] = (int)$card[$i] * $num_x;
$num_i[$i] = $num_i[$i] > 9 ? $num_i[$i] - 9 : $num_i[$i];

}while(isset($card[++$i]));
$num_sum = array_sum($num_i);
return $num_sum % 10 ? False : True;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/766110.htmlTechArticleAccording to the payment card check digit algorithm in ISO 2894 The Luhn Mod-10 Method stipulates: 1. Check the card number Each digit is multiplied by the weight. The rule is that if the number of digits in the card number is an even number, the first...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn