SORTED !!!
One of my problems was in having the variable resetting string prior to the generation loop rather than at the beginning of each cycle within the loop. Doh!
While I was looking at this problem I came across a number of PHP Luhn number validation scripts which all seemed to share a similar mistake. Curious!
All of them only added the doubled (and added if appropriate) digits to the sum for the alternating "odd" digits, rather than then adding the "even" digits.
I am assuming the following definition is correct...
(http://www.webopedia.com/TERM/L/Luhn_formula.html)
1) Starting with the second to last digit and moving left, double the value of all the alternating digits.
2) Starting from the left, take all the unaffected digits and add them to the results of all the individual digits from step 1. If the results from any of the numbers from step 1 are double digits, make sure to add the two numbers first (i.e. 18 would yield 1+8).
3) The total from step 2 must end in zero for the credit-card number to be valid.
For referrence the final working script for my purposes is as follows...
$luhnseed = time();
if (strlen($luhnseed)%2 == 1){
$luhnseed = $luhnseed10;
}
echo "luhnseed = $luhnseed<br />";
$luhnnumber = 0;
while ($luhnnumber == 0){
$sum=0;$digit=0;$first=0;$second=0;$x=0;
for ($x=0; $x<strlen($luhnseed); $x++) {
$digit = substr($luhnseed,$x,1);
if ($x%2 == 0){
$digit = 2;
if (strlen($digit) == 2){
$digit = substr($digit,0,1) + substr($digit,1,1);
}
}
$sum += $digit;
}
if($sum%10!=0){
$luhnseed = $luhnseed+1;
}
else{
$luhnnumber = 1;
}
}
echo "luhn number ($luhnseed) generated<br />sum = $sum<br />";
// test generated
$sum=0;$digit=0;$first=0;$second=0;$x=0;
for ($x=0; $x<strlen($luhnseed); $x++) {
$digit = substr($luhnseed,$x,1);
if ($x%2 == 0){
$digit *= 2;
if (strlen($digit) == 2){
$digit = substr($digit,0,1) + substr($digit,1,1);
}
}
$sum += $digit;
}
if($sum%10!=0){
echo "test number $luhnseed is not luhn<br />sum = $sum<br />";
}
else{
echo "test number $luhnseed is luhn !!!!!!!!!!!!!!!<br />sum = $sum<br />";
}