Hello People, I just started PHP a few days ago so please bear with me. I'm trying to write a credit card validation function recursivly. It works with small number of digits (lets say 4) but when i add more digits (15), the whole thing just screws up. Any ideas?

function sumLuhn($number, $i, $cardlength)
{
$odd = sumOdd($number , $i, $cardlength);

	$even = sumEven($number, $i+1, $cardlength);
	$outLuhn = $even + $odd;
	return $outLuhn;
}
function sumOdd($number, $i, $cardlength)
{
	if ($i<=$cardlength)
	{
		$digit =substr($number, $i*-1,1);
		$outOdd = $digit;
		$outOdd = $outOdd + "sumOdd($number, $i + 2, $cardlength)";
	}
	return $outOdd;

}
function sumEven($number, $i, $cardlength)
{
	if ($i<=$cardlength)
	{
		$digit = substr($number, $i*-1,1);
		if (2*$digit>10 or 2*$digit=10)
		{
			$digit = $digit * 2;
			$digit1 = substr($digit,-1,1);
			$digit = $digit1 + substr($digit,-2,1);
			$digit = $digit/2;
		}

		$outEven = 2*$digit;
		$outEven = $outEven + "sumEven($number, $i + 2, $cardlength";

	}

	return $outEven;
}

    This, 2$digit>10 or 2$digit=10 should probably be:

    if(2*$digit>10 or 2*$digit==10)

    And this:

    $outEven = $outEven + "sumEven($number, $i + 2, $cardlength";

    should probably be:

    $outEven = $outEven + sumEven($number, $i + 2, $cardlength);

    ~Brett

      I tried making those changes but it still won't work. I tried doing echo sumluhn(378765981401955, 1, strlen('378765981401955')). Its supposed to give me 70 but it gave me 27 instead. 😕

        icic. I realized why now. I shoud run echo sumLuhn('378765981401955', 1, strlen('378765981401955')); instead. I didn't put in the single quote marks in. Thanks man.

          Write a Reply...