/ Published in: PHP
URL: http://reusablecode.blogspot.com/2009/03/luhn-algorithm.html
For verifying credit card numbers.
Expand |
Embed | Plain Text
<?php /* Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved. This work is licensed under the Creative Commons Attribution License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. */ // Luhn (mod 10) algorithm function luhn($input) { $sum = 0; // Remove any non-numeric characters. { } // Calculate sum of digits. { $sum += $odd ? $input[$i] : (($input[$i] * 2 > 9) ? $input[$i] * 2 - 9 : $input[$i] * 2); $odd = !$odd; } // Check validity. return ($sum % 10 == 0) ? true : false; } // Unit test - expected result is true. ?>
You need to login to post a comment.
