Implementation of the Luhn algorithm in PHP. Used in validation of credit card numbers and some national identification numbers.

2.0.0 2014-06-01 15:47 UTC


This is an implementation of the Luhn Algorithm in PHP. The Luhn Algorithm is used to validate things like credit cards and national identifcation numbers. More information on the algorithm can be found at Wikipedia


Can be installed using composer:

"nekman/luhn-algorithm": "2.*"


Use the class like this:

$luhn = new \Nekman\LuhnAlgorithm\LuhnAlgorithm('123456789');

The class contains some static functions as well. This will return the Luhn checksum of a number:

$number = '123456789';
$luhnCheckSum = \Nekman\LuhnAlgorithm\LuhnAlgorithm::calculateChecksum($number);


If you'd like to validate the input to the class, extend it and do a regex check. In the file Personnummer.php, I have extended the class to make sure that the input is a valid Swedish national security id.