Php implementation of credit card checkpoint algorithm THELUHNMOD-10 example. According to the ISO2894 payment card checkpoint algorithm TheLuhnMod-10Method rules: 1, each digit on the card number multiplied by the weight. The rule is that if The number of card numbers is an even number, The first rule is according to The Luhn Mod-10 Method of The payment card verification bit algorithm in ISO 2894:
1. multiply each digit on the card number by the weight. The rule is that if the number of card numbers is an even number, the first digit is multiplied by 2; otherwise, the number is multiplied by 1, and then the numbers are, 1, 2, 1, 2;
2. if each digit is multiplied by 9 after the weight, 9 is subtracted;
3. sum all processed weighted numbers and calculate the modulus using number 10;
4. The remainder should be 0, otherwise it may be an input error. It may also be a fake number.
With the simple implementation of PHP, front-end verification is better in actual scenarios, such as JS.
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;
}
The Luhn Mod-10 Method of The payment card check bit algorithm in limit 2894 stipulates: 1. multiply each digit on The card number by The weight. The rule is that if the number of card numbers is an even number, then the first...