PHP code example of designbycode / luhn-algorithm

1. Go to this page and download the library: Download designbycode/luhn-algorithm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

designbycode / luhn-algorithm example snippets


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

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

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

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

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

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

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

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