Created
September 1, 2017 21:44
-
-
Save codycraven/4d46c53c51f8b3e7884d6edaeb746025 to your computer and use it in GitHub Desktop.
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 validCheckDigit($number) { | |
// Reverse digits. | |
$digits = str_split(strrev($number)); | |
$sumDigits = []; | |
foreach ($digits as $key => $value) { | |
// Double every other digit, starting with the check digit. | |
if ($key % 2 !== 0) { | |
// Sum tens and ones place when greater than 9. | |
$value = array_sum(str_split($value * 2)); | |
} | |
$sumDigits[] = $value; | |
} | |
// Calculate the total of all the sum digits. | |
$total = array_sum($sumDigits); | |
// If total modulo 10 is equal to 0, then valid. | |
return $total % 10 === 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment