Luhn algorithm? Verify that the card is valid

Source: Internet
Author: User

Luhn algorithm

1. Starting with the last digit of the card number, the inverse adds the odd digits (1, 3, 5, and so on).

2, starting from the last digit of the card number, the inverse of the even digit number, first multiplied by 2 (if the product is two digits, then subtract 9), and then sum.

3, the sum of odd digits plus even digits sum, the result should be divisible by 10.

function Checkcard ($card) {    $num = 0;    $card = Str_split (Trim ($card));    Krsort ($card);    $i = 1;    foreach ($card as $val) {        if ($i% 2) {//odd            $num + = $val;        } else {//even            $n = $val * 2;            if ($n > 9) $n-= 9;            $num + = $n;        }        $i + +;    }    Return (($num% 10) = = 0);}



Luhn algorithm? Verify that the card is valid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.