Shortening it would pretty much break its usefulness; but it's a few hours later and I've got my library handy. Unfortunately I lost my analysis, but it spots single-digit errors and swapped pairs of digits, and if I recall rightly a few other possible errors.
<?php
class verhoeff{
// Permutations matrix: row I gives the Ith
// power of the permutation (01589427)(36)
var $perm = array(
array(0,1,2,3,4,5,6,7,8,9),
array(1,5,7,6,2,8,3,0,9,4));
//Multiplication table for the fifth dihedral group.
var $d5table = array(
array(0,1,2,3,4,5,6,7,8,9),
array(1,2,3,4,0,6,7,8,9,5),
array(2,3,4,0,1,7,8,9,5,6),
array(3,4,0,1,2,8,9,5,6,7),
array(4,0,1,2,3,9,5,6,7,8),
array(5,9,8,7,6,0,4,3,2,1),
array(6,5,9,8,7,1,0,4,3,2),
array(7,6,5,9,8,2,1,0,4,3),
array(8,7,6,5,9,3,2,1,0,4),
array(9,8,7,6,5,4,3,2,1,0)
);
//d5table[i][inverse[i]]=0
var $inverse = array(0,4,3,2,1,5,6,7,8,9);
function verhoeff(){
for($i=2; $i<8; ++$i)
for($j=0; $j<10; ++$j)
$this->perm[$i][$j] = $this->perm[$i-1][$this->perm[1][$j]];
}
//Function check().
// Returns true if the last digit of the given number is the
// Verhoeff check digit of the rest of the digit sequence.
function check($value)
{ $len = strlen($value)-1;
$check = 0;
for($i=$len; $i>=0; --$i)
$check = $this->d5table[$check][$this->perm[($len-$i)&7][substr($value, $i, 1)]];
return !$check;
}
//Function compute()
// Returns the Verhoeff check digit for the supplied $value
function compute($value)
{ $len = strlen($value);
$check = 0;
for($i=$len-1; $i>=0; --$i)
$check = $this->d5table[$check][$this->perm[($len-$i)&7][substr($value, $i, 1)]];
return $this->inverse[$check];
}
}
/*
Usage:
$verhoeff = new verhoeff();
$number = "1234567";
$checksummed_number = $number . $verhoeff->compute($number);
$number_valid = $verhoeff->check($checksummed_number);
*/
?>