Skip to content

Instantly share code, notes, and snippets.

@codycraven
Created September 1, 2017 21:44
Show Gist options
  • Save codycraven/4d46c53c51f8b3e7884d6edaeb746025 to your computer and use it in GitHub Desktop.
Save codycraven/4d46c53c51f8b3e7884d6edaeb746025 to your computer and use it in GitHub Desktop.
<?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