" . $isbn . " is a good ISBN.
\n";
/* Now figure out the 13 digit equivalent */
$newisbn = substr($isbn, -10, 9); // drop the checkdigit
$newisbn = "978" . $newisbn; // Add 978 prefix
/* Run through the new first 12 digits, weighting them
alternately by 1 and 3, sum them up */
$sum = $newisbn[0] + ($newisbn[1] * 3) + $newisbn[2] + ($newisbn[3] * 3) +
$newisbn[4] + ($newisbn[5] * 3) + $newisbn[6] + ($newisbn[7] * 3) +
$newisbn[8] + ($newisbn[9] * 3) + $newisbn[10] + ($newisbn[11] * 3);
/* Get the remainder when the sum is divided by 10 */
$mod = $sum % 10;
/* Calculate the new checkdigit */
$checkdigit = 10 - $mod;
/* If $checkdigit is 10, change it to 0. Otherwise leave as-is. */
$checkdigit = ($checkdigit == "10") ? "0" : $correct_checkdigit;
/* Append it to the first 12 digits */
$newisbn = $newisbn . $checkdigit;
/* Print it out */
print "When converted to ISBN-13, " . $isbn . " becomes " . $newisbn . ".
\n" . 'Look for this publication on Amazon.com' . "
\n\n"; print "\nWould you like to try another?
\n\n"; display_form(); } // end if checking to see if the 10 digit isbn was good else { display_error("bad checkdigit"); // If not, display an error } break; case "13": // If the isbn is 13 digits, do the following /* Run through the new first 12 digits, weighting them alternately by 1 and 3, sum them up */ $sum = $isbn[0] + ($isbn[1] * 3) + $isbn[2] + ($isbn[3] * 3) + $isbn[4] + ($isbn[5] * 3) + $isbn[6] + ($isbn[7] * 3) + $isbn[8] + ($isbn[9] * 3) + $isbn[10] + ($isbn[11] * 3); /* Get the remainder when the sum is divided by 10 */ $mod = $sum % 10; /* Calculate the new checkdigit */ $correct_checkdigit = 10 - $mod; /* If $checkdigit is 10, change it to 0. Otherwise leave as-is. */ $correct_checkdigit = ($correct_checkdigit == "10") ? "0" : $correct_checkdigit; /* Compare the two checkdigits */ if ($checkdigit == $correct_checkdigit) { print "\n" . $isbn . " is a valid ISBN.
\n\n"; // Success print "\n" . 'Look for this publication on Amazon.com' . "
\n\n"; print "\nWould you like to try another?
\n\n"; display_form(); } else { display_error("bad checkdigit"); } break; default: // If the isbn is neither 10 nor 13 digits, display an error display_error("invalid length"); break; } // end switch } // end if to check for posted data /* If the form hasn't been submitted, display it. */ else { display_form(); } function display_error($error_type) { ?>Sorry, the characters of the ISBN you entered that must be numeric are not numeric. Please try again. Sorry, ISBNs only come in lengths of 10 and 13. If you entered any data other than the digits 1-9, hyphens, spaces, or the letter X, please try re-entering your request without those extraneous characters. Sorry, the ISBN you entered has a bad checkdigit. Please try again with another ISBN candidate. You shouldn't see this error.
Please note: If you are entering an old SBN number with only 9 digits you must add a "0" to the beginning of your entry.