Created
October 14, 2011 18:24
-
-
Save troelskn/1287893 to your computer and use it in GitHub Desktop.
Luhn's algorithm in php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function is_valid_luhn($number) { | |
settype($number, 'string'); | |
$sumTable = array( | |
array(0,1,2,3,4,5,6,7,8,9), | |
array(0,2,4,6,8,1,3,5,7,9)); | |
$sum = 0; | |
$flip = 0; | |
for ($i = strlen($number) - 1; $i >= 0; $i--) { | |
$sum += $sumTable[$flip++ & 0x1][$number[$i]]; | |
} | |
return $sum % 10 === 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, this is my implementation: https://github.com/danielebarbaro/laravel-vat-eu-validator/blob/26e65de7df083e729b44e0ece02483dc6076bc71/src/VatValidator.php#L124
it works since 2019 🤓