Skip to content

Instantly share code, notes, and snippets.

@mikestratton
Created April 7, 2015 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikestratton/a8b1294444ae3611ff01 to your computer and use it in GitHub Desktop.
Save mikestratton/a8b1294444ae3611ff01 to your computer and use it in GitHub Desktop.
ISBN-10 Validation Written in PHP
<?php
echo"Enter the ISBN number: ";
$isbn = trim(fgets(STDIN));
$isbn_length = strlen($isbn); //length
if (10 != $isbn_length) //ISBN length = 10?
{ echo "Not Valid";
exit;
}
$arr1 = str_split($isbn); // to array
$m0 = $arr1[0] * 10;
$m1 = $arr1[1] * 9;
$m2 = $arr1[2] * 8;
$m3 = $arr1[3] * 7;
$m4 = $arr1[4] * 6;
$m5 = $arr1[5] * 5;
$m6 = $arr1[6] * 4;
$m7 = $arr1[7] * 3;
$m8 = $arr1[8] * 2;
$m9 = $arr1[9] * 1;
$total = $m0+$m1+$m2+$m3+$m4+$m5+$m6+$m7+$m8+$m9;
if($total %11 != 0)
{ echo "Not Valid";
exit;
}
else {echo "Valid";}
exit;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment