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

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

designbycode/luhn-algorithm
===========================

The Luhn Algorithm is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, and others. This documentation provides an overview of the Luhn Algorithm implementation in PHP, including examples and use cases.

v1.0.1(1y ago)1328↓100%[1 PRs](https://github.com/designbycode/luhn-algorithm/pulls)2MITPHPPHP ^8.1|^8.2|^8.3

Since Jun 19Pushed 1y agoCompare

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

READMEChangelog (2)Dependencies (3)Versions (4)Used By (2)

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

[](#luhn-algorithm)

[![Latest Version on Packagist](https://camo.githubusercontent.com/bdc858e56c795d52a460c7a31e9aaa7cdfd49a1488bce789fb90c300baba26d0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64657369676e6279636f64652f6c75686e2d616c676f726974686d2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/designbycode/luhn-algorithm)[![Tests](https://camo.githubusercontent.com/bf1fd5e9d849e360573311519dc43afde617bddbd8617a71f54e9ad4aab0c05f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f64657369676e6279636f64652f6c75686e2d616c676f726974686d2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/designbycode/luhn-algorithm/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/118318285d78bcc263a7f36ee16689295c6db673b24b6d0c986a48b6bdb9e942/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64657369676e6279636f64652f6c75686e2d616c676f726974686d2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/designbycode/luhn-algorithm)

The Luhn Algorithm is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, and others. This documentation provides an overview of the Luhn Algorithm implementation in PHP, including examples and use cases.

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

[](#installation)

You can install the package via composer:

```
composer require designbycode/luhn-algorithm
```

Usage
-----

[](#usage)

### Validating a Value

[](#validating-a-value)

To validate a value using the Luhn Algorithm, create an instance of the LuhnAlgorithm class and call the isValid method:

```
$luhn = new LuhnAlgorithm();
$isValid = $luhn->isValid('79927398713'); // returns true
```

The isValid method returns `true` if the value is valid, and `false` otherwise.

Generating a Check Digit
------------------------

[](#generating-a-check-digit)

To generate a check digit for a given value, call the `generate` method:

```
$luhn = new LuhnAlgorithm();
$generatedCheckDigit = $luhn->generate('7992739871'); // returns '79927398713'
```

The `generate` method returns the generated value including the check digit.

Stripping the Check Digit
-------------------------

[](#stripping-the-check-digit)

To strip the check digit from a given value, call the `withoutDigit` method:

```
$luhn = new LuhnAlgorithm();
$valueWithoutDigit = $luhn->withoutDigit('79927398713'); // returns '7992739871'
```

The `withoutDigit` method returns the value without the check digit.

Validating and Suggesting a Check Digit
---------------------------------------

[](#validating-and-suggesting-a-check-digit)

To validate a value and suggest the correct check digit if invalid, call the `validateAndSuggest` method:

```
$luhn = new LuhnAlgorithm();
$result = $luhn->validateAndSuggest('79927398712'); // returns ['isValid' => false, 'uggestedDigit' => '3']
```

The `validateAndSuggest` method returns an array containing `isValid` and `suggestedDigit` values.

Use Cases
---------

[](#use-cases)

### Credit Card Validation

[](#credit-card-validation)

The Luhn Algorithm is commonly used to validate credit card numbers. You can use the `isValid` method to check if a credit card number is valid:

```
$luhn = new LuhnAlgorithm();
$isValid = $luhn->isValid('4242424242424242'); // returns true
```

### IMEI Number Validation

[](#imei-number-validation)

The Luhn Algorithm can also be used to validate IMEI numbers. You can use the `isValid` method to check if an IMEI number is valid:

```
$luhn = new LuhnAlgorithm();
$isValid = $luhn->isValid('352656091234567'); // returns true
```

### Generating Check Digits for IDs

[](#generating-check-digits-for-ids)

You can use the `generate` method to generate check digits for various identification numbers, such as customer IDs or product codes:

```
$luhn = new LuhnAlgorithm();
$generatedCheckDigit = $luhn->generate('CUSTOMER1234'); // returns 'CUSTOMER12345'
```

Error Handling
--------------

[](#error-handling)

The Luhn Algorithm implementation throws an `InvalidArgumentException` if the input value is empty or invalid. You can catch this exception to handle errors:

```
try {
    $luhn = new LuhnAlgorithm();
    $luhn->isValid('');
} catch (InvalidArgumentException $e) {
    echo 'Error: '. $e->getMessage();
}
```

Conclusion
----------

[](#conclusion)

The Luhn Algorithm implementation in PHP provides a simple and efficient way to validate and generate check digits for various identification numbers. By following this documentation, you can easily integrate the Luhn Algorithm into your PHP applications.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [claudemyburgh](https://github.com/claudemyburgh)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 62.5% 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.

###  Release Activity

Cadence

Every ~31 days

Total

2

Last Release

658d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/65820d809cbae9f6fa2f5984f3c584e97b8fad0824590cf23e36cc8d85166cb2?d=identicon)[claudemyburgh](/maintainers/claudemyburgh)

---

Top Contributors

[![claudemyburgh](https://avatars.githubusercontent.com/u/6057076?v=4)](https://github.com/claudemyburgh "claudemyburgh (5 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (1 commits)")

---

Tags

designbycodeluhn-algorithm

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

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

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

###  Alternatives

[webmozart/assert

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

7.6k894.0M1.2k](/packages/webmozart-assert)[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)[xemlock/htmlpurifier-html5

HTML5 support for HTML Purifier

1052.9M11](/packages/xemlock-htmlpurifier-html5)

PHPackages © 2026

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