PHPackages                             nekman/luhn-algorithm - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. nekman/luhn-algorithm

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

nekman/luhn-algorithm
=====================

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

5.0.2(11mo ago)22819.4k↓14.2%5MITPHPPHP ^7.4||^8.0

Since Mar 13Pushed 6mo agoCompare

[ Source](https://github.com/Ekman/Luhn-Algorithm)[ Packagist](https://packagist.org/packages/nekman/luhn-algorithm)[ RSS](/packages/nekman-luhn-algorithm/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (15)Used By (0)

Luhn Algorithm
==============

[](#luhn-algorithm)

[![Build Status](https://camo.githubusercontent.com/37cf5479f324bec0d4639c04b227f5f3ca03c26909cb16d4ad559e2e08ebc2b5/68747470733a2f2f636972636c6563692e636f6d2f67682f456b6d616e2f6c75686e2d616c676f726974686d2e7376673f7374796c653d737667)](https://app.circleci.com/pipelines/github/Ekman/luhn-algorithm)[![Coverage Status](https://camo.githubusercontent.com/fa73ff5cc8885bcd4dc0dcca6b0187b464e4839a16f4d056973c840158104b22/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f456b6d616e2f6c75686e2d616c676f726974686d2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/Ekman/luhn-algorithm?branch=master)

This is a zero dependency implementation of the Luhn Algorithm for PHP 7.4 and above. The Luhn Algorithm is used to validate things like credit cards and national identification numbers. More information on the algorithm can be found at [Wikipedia](http://en.wikipedia.org/wiki/Luhn_algorithm).

Installation
------------

[](#installation)

Install with [Composer](https://getcomposer.org/):

```
composer require nekman/luhn-algorithm
```

Usage
-----

[](#usage)

In order to instantiate a new instance of the library, use the factory:

```
use Nekman\LuhnAlgorithm\LuhnAlgorithmFactory;

$luhn = LuhnAlgorithmFactory::create();
```

You can find [the library facade in the `LuhnAlgorithmInterface.php` file](src/Contract/LuhnAlgorithmInterface.php).

[The `Number` class](src/Number.php) is a container class that holds the actual number and the check digit. It does no validation nor does it calculate the check digit. It exists to clearly separate the number from the check digit and to define when the check digit exists or not. To simplify the process of validating a number you can use the named constructor `Number::fromString()` like this:

```
use Nekman\LuhnAlgorithm\Number;

// Assume $creditCard is from a form.
$number = Number::fromString($creditCard);

if ($luhn->isValid($number)) {
    // Number is valid.
}
```

Alternatively, if you want to calculate the checksum or check digit for a number:

```
use Nekman\LuhnAlgorithm\Number;

$number = new Number(12345);

$checksum = $luhn->calcChecksum($number);

$checkDigit = $luhn->calcCheckDigit($number);
```

Versioning
----------

[](#versioning)

This project complies with [Semantic Versioning](https://semver.org/).

Changelog
---------

[](#changelog)

For a complete list of changes, and how to migrate between major versions, see [releases page](https://github.com/Ekman/luhn-algorithm/releases).

Buy me a coffee
---------------

[](#buy-me-a-coffee)

[!["Buy Me A Coffee"](https://camo.githubusercontent.com/9f44ce2dc3b3eecdd02598900866ffc518801df1932849703dae1e5ce5031070/68747470733a2f2f7777772e6275796d6561636f666665652e636f6d2f6173736574732f696d672f637573746f6d5f696d616765732f6f72616e67655f696d672e706e67)](https://buymeacoffee.com/nekman)

If you appreciate my work, then consider [buying me a coffee](https://buymeacoffee.com/nekman). Donations are completely voluntary.

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance60

Regular maintenance activity

Popularity48

Moderate usage in the ecosystem

Community14

Small or concentrated contributor base

Maturity76

Established project with proven stability

 Bus Factor3

3 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~316 days

Recently: every ~599 days

Total

14

Last Release

337d ago

Major Versions

1.0.0 → 2.0.02014-06-01

2.0.1 → 3.0.02018-02-19

3.0.0 → 4.0.02018-02-26

4.1.0 → 5.0.02021-01-10

PHP version history (4 changes)1.0.0PHP &gt;=4.0.0

3.0.0PHP ^7.0

4.0.0PHP ^7.1

5.0.0PHP ^7.4||^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/568019?v=4)[Nils](/maintainers/NEkman)[@nekman](https://github.com/nekman)

---

Top Contributors

[![Ekman](https://avatars.githubusercontent.com/u/1544220?v=4)](https://github.com/Ekman "Ekman (1 commits)")[![Horat1us](https://avatars.githubusercontent.com/u/2751367?v=4)](https://github.com/Horat1us "Horat1us (1 commits)")[![lindyhopchris](https://avatars.githubusercontent.com/u/4464333?v=4)](https://github.com/lindyhopchris "lindyhopchris (1 commits)")[![p16](https://avatars.githubusercontent.com/u/272483?v=4)](https://github.com/p16 "p16 (1 commits)")[![vanodevium](https://avatars.githubusercontent.com/u/16780069?v=4)](https://github.com/vanodevium "vanodevium (1 commits)")

---

Tags

cardcreditcreditcardcreditcard-validatorluhnluhn-algorithmluhn-checksumluhn-phppersonnummerphpvalidationcardidentificationluhncredit

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/nekman-luhn-algorithm/health.svg)

```
[![Health](https://phpackages.com/badges/nekman-luhn-algorithm/health.svg)](https://phpackages.com/packages/nekman-luhn-algorithm)
```

###  Alternatives

[composer/semver

Version comparison library that offers utilities, version constraint parsing and validation.

3.3k489.6M672](/packages/composer-semver)[giggsey/libphonenumber-for-php

A library for parsing, formatting, storing and validating international phone numbers, a PHP Port of Google's libphonenumber.

5.0k148.7M416](/packages/giggsey-libphonenumber-for-php)[respect/validation

The most awesome validation engine ever created for PHP

5.9k37.4M383](/packages/respect-validation)[propaganistas/laravel-phone

Adds phone number functionality to Laravel based on Google's libphonenumber API.

3.0k35.7M107](/packages/propaganistas-laravel-phone)[intervention/validation

Additional validation rules for the Laravel framework

6826.7M8](/packages/intervention-validation)[opis/json-schema

Json Schema Validator for PHP

64736.9M186](/packages/opis-json-schema)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
