TheJoey Posted October 11, 2009 Share Posted October 11, 2009 hey i need to rewrite this if (luhn_check($number)) to include is_numeric though im having trouble when i use it as if (luhn_check(is_numeric($cc))) im guessing it cant be done that way, but i really need to implement it similar Quote Link to comment https://forums.phpfreaks.com/topic/177277-rewrite-function-help/ Share on other sites More sharing options...
.josh Posted October 11, 2009 Share Posted October 11, 2009 is_numeric returns true or false, so you end up passing true or false to luhn_check instead of the actual value. You need to do something more like this: if (is_numeric($cc) && luhn_check($cc)) Although...depending on what this luhn_check() function of yours does, it may not even be necessary... what does it do? Is it checking it to be a certain format already? Quote Link to comment https://forums.phpfreaks.com/topic/177277-rewrite-function-help/#findComment-934712 Share on other sites More sharing options...
TheJoey Posted October 11, 2009 Author Share Posted October 11, 2009 luhn check is a credit card functionality it works fine, but its not giving me errors for letters and such. <?php /* Luhn algorithm number checker - (c) 2005-2008 - planzero.org * * This code has been released into the public domain, however please * * give credit to the original author where possible. */ function luhn_check($number) { // Strip any non-digits (useful for credit card numbers with spaces and hyphens) $number=preg_replace('/\D/', '', $number); // Set the string length and parity $number_length=strlen($number); $parity=$number_length % 2; // Loop through each digit and do the maths $total=0; for ($i=0; $i<$number_length; $i++) { $digit=$number[$i]; // Multiply alternate digits by two if ($i % 2 == $parity) { $digit*=2; // If the sum is two digits, add them together (in effect) if ($digit > 9) { $digit-=9; } } // Total up the digits $total+=$digit; } // If the total mod 10 equals 0, the number is valid return ($total % 10 == 0) ? TRUE : FALSE; } ?> if (is_numeric($cc) && luhn_check($cc)) $error = false; else $error = true; seems to be giving me error regardless Quote Link to comment https://forums.phpfreaks.com/topic/177277-rewrite-function-help/#findComment-934713 Share on other sites More sharing options...
mikesta707 Posted October 11, 2009 Share Posted October 11, 2009 what is the error? btw you can just do this return ($total % 10 == 0); Quote Link to comment https://forums.phpfreaks.com/topic/177277-rewrite-function-help/#findComment-934718 Share on other sites More sharing options...
TheJoey Posted October 11, 2009 Author Share Posted October 11, 2009 its giving me the error ive programmed it to say. Even if the credit card is valid. Quote Link to comment https://forums.phpfreaks.com/topic/177277-rewrite-function-help/#findComment-934729 Share on other sites More sharing options...
mikesta707 Posted October 11, 2009 Share Posted October 11, 2009 if (is_numeric($cc) && luhn_check($cc)) $error = false; else $error = true; well asuming you want error to be true when the luhn check is true and isnumeric is true, than the logic is wrong. right now, if the cc number is numeric, and the luhn check runs true, $error will be false. try if (!is_numeric($cc) || !luhn_check($cc)) $error = false; else $error = true; Quote Link to comment https://forums.phpfreaks.com/topic/177277-rewrite-function-help/#findComment-934735 Share on other sites More sharing options...
TheJoey Posted October 11, 2009 Author Share Posted October 11, 2009 if (!is_numeric($cc) && !luhn_check($cc)) $errorCC = true; else $errorCC = false; this checks the algorithmn fine just doesnt check for numeric if (!is_numeric($cc) || !luhn_check($cc)) $errorCC = true; else $errorCC = false; this works well with numeric but doesnt use the luhn check well.. Lol i dont know wat to do. Quote Link to comment https://forums.phpfreaks.com/topic/177277-rewrite-function-help/#findComment-934746 Share on other sites More sharing options...
TheJoey Posted October 11, 2009 Author Share Posted October 11, 2009 if (!is_numeric($cc) && !luhn_check($cc)) $errorCC = true; else $errorCC = false; this checks the algorithmn fine just doesnt check for numeric if (!is_numeric($cc) || !luhn_check($cc)) $errorCC = true; else $errorCC = false; this works well with numeric but doesnt use the luhn check well.. Lol i dont know wat to do. Quote Link to comment https://forums.phpfreaks.com/topic/177277-rewrite-function-help/#findComment-934840 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.