Download the PHP package designbycode/luhn-algorithm without Composer
On this page you can find all versions of the php package designbycode/luhn-algorithm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download designbycode/luhn-algorithm
More information about designbycode/luhn-algorithm
Files in designbycode/luhn-algorithm
Package luhn-algorithm
Short Description 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.
License MIT
Homepage https://github.com/designbycode/luhn-algorithm
Informations about the package luhn-algorithm
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
You can install the package via composer:
Usage
Validating a Value
To validate a value using the Luhn Algorithm, create an instance of the LuhnAlgorithm class and call the isValid method:
The isValid method returns true
if the value is valid, and false
otherwise.
Generating a Check Digit
To generate a check digit for a given value, call the method:
The generate
method returns the generated value including the check digit.
Stripping the Check Digit
To strip the check digit from a given value, call the withoutDigit
method:
The withoutDigit
method returns the value without the check digit.
Validating and Suggesting a Check Digit
To validate a value and suggest the correct check digit if invalid, call the validateAndSuggest
method:
The validateAndSuggest
method returns an array containing isValid
and suggestedDigit
values.
Use Cases
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:
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:
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:
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:
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
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- claudemyburgh
- All Contributors
License
The MIT License (MIT). Please see License File for more information.