The calculation method of ID card check code based on PHP

Source: Internet
Author: User
Tags php language
This article to share the attempt to use the PHP language to implement the ID check code to determine whether the user's ID number is correct

China (mainland) Citizen ID card number each one represents the meaning, many articles on the internet have been introduced, there is not much to say. The last digit of the identity card number is the check code, which is calculated according to the first 17 digits. The algorithm is like this: the first 17 bits of each number and a series of weighting factors, and then calculate the sum of these products, the sum of these products and modulo 11 as a sequence number, and finally in a check code string to extract the character corresponding to the sequence number. Of course, there are many articles on the internet to teach you to calculate this check code, below we will try to use the PHP language to do this work, perhaps can be used in PHP development, such as verifying the user's identity card number is correct.

Suppose that the first 17 digits of a Chinese citizen's ID number are this: 44010221990101001 (note: This person was born in 2199), then we followed the above algorithm to write a few lines of PHP code to complete the verification code calculation. To make it easier for everyone to understand, I used a simpler statement, see the code:

<?php//ID number top 17, can be obtained from various data sources (such as database, user submitted form, etc.) $body = ' 44010221990101001 ';//weighted factor $wi = Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);//Check code string $ai = Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ');//sequentially cycle through the first 3 bits for ($i = 0;$ I < $i + +) {//extract one of the first 17 bits and convert the variable type to real $b = (int) $body {$i};//extract the corresponding weighting factor $w = $wi [$i];//multiplies a number and weighting factor extracted from the ID number and accumulates $ Sigma + = $b * $W;} Calculated ordinal # = $sigma% 11;//extracts the corresponding characters from the checksum string by ordinal. $check _number = $ai [$number];//output echo $body. $check _number;? >

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

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

<?php$body = ' 44010221990101001 '; $wi = Array (7, 9, 5, 8, 4, 2, 1, 6, 3, 7, 9, ten, 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)];? >

Summary: The above is the entire content of this article, I hope to be able to help you learn.

Related Article

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.