# File: Luhn.php

 Recommend this page to a friend! ReTweet
 Classes of Rolands Kusins > PHP Luhn Algorithm > Luhn.php > Download
File: Luhn.php Class source text/plain Luhn check digit calculation class PHP Luhn Algorithm Calculate and validate a Luhn check digit By Rolands Kusins New calculation algorithm, see http://www.phpclasses.org/discuss/package/8471/thread/1/ 2 years ago 2,203 bytes

## Contents

 ``` =0;--\$i) \$sum += \$this->sumTable[\$flip++ & 0x1][\$number[\$i]];         // Multiply by 9         \$sum *= 9;         // Last digit of sum is check digit         return (int)substr(\$sum,-1,1);     }         /**      * Calculate check digit according to Luhn's algorithm      * This is an old method, tests show that this is little bit slower than new one      *      * @param string \$number      * @return integer      */     public function calculateOld(\$number){         \$length = strlen(\$number);         \$sum = 0;         \$p = \$length % 2;         // Sum digits, where every second digit from right is doubled (last one is check digit, which is not in parameter)         for(\$i=\$length-1;\$i>=0;--\$i){             \$digit = \$number[\$i];             // Every second digit is doubled             if(\$i % 2 != \$p){                 \$digit *= 2;                 // If doubled value is 10 or more (for example 13), then add to sum each digit (i.e. 1 and 3)                 if(\$digit > 9){                     \$sum += \$digit[0];                     \$sum += \$digit[1];                 } else{                     \$sum += \$digit;                 }             } else{                 \$sum += \$digit;             }         }         // Multiply by 9         \$sum *= 9;         // Last one is check digit         return (int)substr(\$sum,-1,1);     }         /**      * Validate number against check digit      *      * @param string \$number      * @param integer \$digit      * @return boolean      */     public function validate(\$number,\$digit){         \$calculated = \$this->calculate(\$number);         if(\$digit == \$calculated) return true;         else return false;     } } ?> ```