Example of PHP calculation ID check code

Source: Internet
Author: User
Tags php language php code

China (mainland) Citizen ID number each representative of the meaning of the Internet, many articles are introduced, this is not much to say. The last one of the ID card number is the check code, according to the first 17 digits calculated. The algorithm is probably this: the first 17 digits of each number and a series of weighted factors, and then calculate the sum of these products, the sum of these products and modulo 11 as the number of numbers, and finally in a check code string to extract the corresponding sequence of characters. Of course, there are many articles on the internet to teach you to calculate this check code, we will try to use the PHP language to complete this work, may be used in PHP development, such as verifying the user's identity card number is correct.

Suppose a Chinese (mainland) Citizen's ID number 17 is this: 44010221990101001 (note: This person was born in 2199), then we follow the above algorithm to try to write a few lines of PHP code to complete the checkout code calculation. To make it easier for everyone to understand, I used a simpler statement, see the code:

  code is as follows copy code

<?php

//ID number 17, available from various data sources (such as database, user submitted form, etc.)

$body   = ' 44010221990101001 ';

//Weighting factor

$wi = Array (7, 9, 5, 8, 4, 2, 1, 6, 3, 7, 9, ten, 5, 8, 4, 2);

//Checksum code string

$ai = Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 ');

//Cycle first 17 bits in order

for ($i = 0; $i < $i + +) {

///Extract one of the first 17 digits and convert the variable type to a real number

$b = (int) $body {$i};

//Extract the appropriate weighting factor

$w = $wi [$i];

//Multiplies a number and weighted factor extracted from the ID card number and adds

$sigma + + $b * $W;

}

//Count ordinal

$number = $sigma%;

//The corresponding character is extracted from the check code string by ordinal.

$check _number = $ai [$number];

//Output

Echo $body. $check _number;

?

After running the above code, you can figure out that the verification code for the ID card is 9. You can try the top 17 of your ID card.

If you understand the above example, you can combine some of the statements in this code, remove unnecessary variables, and optimize the following code:

The code is as follows Copy Code

<?php

$body = ' 44010221990101001 ';

$wi = Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);

$ai = Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 ');

for ($i = 0; $i < $i + +) {

$sigma + = ((int) $body {$i}) * $wi [$i];

}

echo $body. $ai [($sigma% 11)];

?>

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.