 # File: Luhn.php Download
 ``` = 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;                     \$sum += \$digit;                 } 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;     } } ?> ``` Advertise on this site Site map Newsletter Statistics Site tips Privacy policy Contact
For more information send a message to `info at phpclasses dot org`.