Home  >  Article  >  Backend Development  >  PHP implements the Luhn algorithm to verify whether the credit card number is valid_PHP tutorial

PHP implements the Luhn algorithm to verify whether the credit card number is valid_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:00:34889browse

PHP implements the Luhn algorithm to verify whether the credit card number is valid

This article mainly introduces the PHP implementation to verify whether the credit card number is valid through the Luhn algorithm. An example analysis of the PHP implementation of Luhn Algorithms and related application skills have certain reference value. Friends in need can refer to it

The example in this article describes how PHP implements the Luhn algorithm to verify whether a credit card number is valid. Share it with everyone for your reference. The specific implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

$numbers = "49927398716 49927398717 1234567812345678 1234567812345670";

foreach (split(' ', $numbers) as $n)

echo "$n is ", luhnTest($n) ? 'valid' : 'not valid', '
';

 

function luhnTest($num) {

$len = strlen($num);

for ($i = $len-1; $i >= 0; $i--) {

$ord = ord($num[$i]);

if (($len - 1) & $i) {

$sum = $ord;

} else {

$sum = $ord / 5 (2 * $ord) % 10;

}

}

return $sum % 10 == 0;

}

1 2

3

4

5

6

1

2

3

4

49927398716 is valid

49927398717 is not valid

1234567812345678 is not valid

1234567812345670 is valid

7 8

9

10

11

12

13
14

15

16

$numbers = "49927398716 49927398717 1234567812345678 1234567812345670";

foreach (split(' ', $numbers) as $n)

1

2

3

4

49927398716 is valid

49927398717 is not valid

1234567812345678 is not valid

1234567812345670 is valid

echo "$n is ", luhnTest($n) ? 'valid' : 'not valid', '
';

function luhnTest($num) { $len = strlen($num);

for ($i = $len-1; $i >= 0; $i--) {

if (($len - 1) & $i) { $sum = $ord; } else { $sum = $ord / 5 (2 * $ord) % 10; }
}
return $sum % 10 == 0;
}
Run results ?
1 2 3 4 49927398716 is valid 49927398717 is not valid 1234567812345678 is not valid 1234567812345670 is valid
Here is a more concise code: The code is as follows: function luhn_test($num) { $str = ''; foreach( array_reverse( str_split( $num ) ) as $i => $c ) $str .= ($i % 2 ? $c * 2 : $c ); return array_sum( str_split($str) ) % 10 == 0; } foreach (array('49927398716','49927398717','1234567812345678','1234567812345670') as $n) echo "$n is ", luhn_test($n) ? 'valid' : 'not valid', "
n"; The output results are as follows ?
1 2 3 4 49927398716 is valid 49927398717 is not valid 1234567812345678 is not valid 1234567812345670 is valid
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/973138.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/973138.htmlTechArticlePHP implements verification of credit card number through Luhn algorithm. This article mainly introduces PHP implementation of verification through Luhn algorithm. Whether the credit card number is valid, an example analysis of PHP's implementation of Luhn algorithm and...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn