PHPackages                             thewebsolver/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. thewebsolver/luhn-algorithm

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

thewebsolver/luhn-algorithm
===========================

PHP library to validate digits such as Credit Card Number, IMEI number, etc. with Luhn Algorithm

03PHP

Since Aug 19Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/TheWebSolver/luhn-algorithm)[ Packagist](https://packagist.org/packages/thewebsolver/luhn-algorithm)[ RSS](/packages/thewebsolver-luhn-algorithm/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

📦 This package is a sub-repo split of a PHP Validator which is currently under development.

✨ This can also be installed as a dependency for [Payment Card](https://github.com/thewebsolver/payment-card) library to validate Debit/Credit Cards.

Introduction
============

[](#introduction)

Luhn Algorithm to validate Payment Cards, IMEI numbers and other digits that needs Luhn validation.

Usage
-----

[](#usage)

### Install

[](#install)

```
composer require thewebsolver/luhn-algorithm
```

### Validate

[](#validate)

Validation can be performed using any of the three OPTIONS presented below:

```
use TheWebSolver\Codegarage\Validator\LuhnAlgorithm;

// OPTION 1: Value passing via constructor.
$luhn     = new LuhnAlgorithm(79927398713);
$isValid  = $luhn->isValid();  // true
$checksum = $luhn->checksum(); // 70

// OPTION 2: Value passing via invocable class.
$luhn     = new LuhnAlgorithm();
$isValid  = $luhn(79927398713); // true
$checksum = $luhn->checksum();  // 70

// OPTION 3: Value passing via static method. In this case, There is no
// instance stored in memory & can only be used for one-off validation.
$isValid = LuhnAlgorithm::validate(79927398713); // true
```

### Debug

[](#debug)

From above [Validation](#validate) example, we can debug more details about the validation status and various state of each number when checkum was calculated.

```
var_dump( $luhn ); // Input value was: 79927398713

// The debug output.
array(4) {
  'isValid' =>
  bool(true)
  'digits' =>
  int(79947697723)
  'checksum' =>
  int(70)
  'state' =>
  array(11) {
    [0] =>
    array(2) {
      'doubled' =>
      bool(false)
      'result' =>
      int(7)
    }
    [1] =>
    array(2) {
      'doubled' =>
      bool(true)
      'result' =>
      int(9)
    }
    [2] =>
    array(2) {
      'doubled' =>
      bool(false)
      'result' =>
      int(9)
    }
    [3] =>
    array(2) {
      'doubled' =>
      bool(true)
      'result' =>
      int(4)
    }
    [4] =>
    array(2) {
      'doubled' =>
      bool(false)
      'result' =>
      int(7)
    }
    [5] =>
    array(2) {
      'doubled' =>
      bool(true)
      'result' =>
      int(6)
    }
    [6] =>
    array(2) {
      'doubled' =>
      bool(false)
      'result' =>
      int(9)
    }
    [7] =>
    array(2) {
      'doubled' =>
      bool(true)
      'result' =>
      int(7)
    }
    [8] =>
    array(2) {
      'doubled' =>
      bool(false)
      'result' =>
      int(7)
    }
    [9] =>
    array(2) {
      'doubled' =>
      bool(true)
      'result' =>
      int(2)
    }
    [10] =>
    array(2) {
      'doubled' =>
      bool(false)
      'result' =>
      int(3)
    }
  }
}
```

Composition
===========

[](#composition)

The [Luhn Algorithm](Src/LuhnAlgorithm.php) class is actually composed by using [Luhn](Src/Luhn.php) trait. This trait can be used elsewhere in separate class defined in your project or modify validation methods as per the project's requirement (latter of which seem unnecessary though).

###  Health Score

16

—

LowBetter than 5% of packages

Maintenance41

Moderate activity, may be stable

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity13

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/6921441544aa2b10de561f0bc8a785365ed6932f709ead6a6f31d7e2290e4c8c?d=identicon)[hsehszroc](/maintainers/hsehszroc)

---

Top Contributors

[![hsehszroc](https://avatars.githubusercontent.com/u/48177731?v=4)](https://github.com/hsehszroc "hsehszroc (13 commits)")

---

Tags

bank-cardcredit-carddebit-cardimeiimei-validationluhnluhn-algorithmluhn-checkdigitluhn-checksumluhn-phpluhn-validationpayment-cardvalidationvalidator

### Embed Badge

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

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

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

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