Home > Article > Backend Development > PHP implements the Luhn algorithm to verify whether the credit card number is valid_PHP tutorial
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:
?
3 4
5
6
9 10 11 12 13 14 15 16
|
$numbers = "49927398716 49927398717 1234567812345678 1234567812345670"; return $sum % 10 == 0;foreach (split(' ', $numbers) as $n)
function luhnTest($num) { $len = strlen($num); for ($i = $len-1; $i >= 0; $i--) { } |
1 2 3 4 | 49927398716 is valid 49927398717 is not valid 1234567812345678 is not valid 1234567812345670 is valid |
1 2 3 4 | 49927398716 is valid 49927398717 is not valid 1234567812345678 is not valid 1234567812345670 is valid |