Nothing to say, just make a backup ~
---------------------------------------- I am a split line ---------------------------------------------
According to the Payment Card checkpoint in ISO 2894AlgorithmThe luhn mod-10 method rules:
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.
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 ;}
PS: What should I do if I have a preliminary interview question ~