Tag: PHP UPC Check Digit

PHP Script for Calculating UPC Code Check Digit

Here’s a php function (function calculate_upc_check_digit($upc_code)) I wrote today for calculating the check digit of a UPC-A (12 digit) UPC Code. You can use it to validate a UPC Code or to calculate the check digit when the manufacturer only supplies 11 digits of the UPC code.

The script will accept a UPC Code of length 11 or 12, and return the calulated check digit. If the length of the UPC Code is other then 11 or 12 it returns -1.


// ----------------------------------------------------
// calculate check digit for upc
// parameter UPC can be either 11 digits (to calculate)
// or full 12 digits to use to validate
// returns -1 on failure
// or check digit 0 - 9 on success
// to validate upc send 12 digit upc code in
// and compare returned value with the 12th
// digit of UPC code you are validating
// --------------------------------------------------
function calculate_upc_check_digit($upc_code) {
$checkDigit = -1; // -1 == failure
$upc = substr($upc_code,0,11);
// send in a 11 or 12 digit upc code only
if (strlen($upc) == 11 && strlen($upc_code) <= 12) { $oddPositions = $upc[0] + $upc[2] + $upc[4] + $upc[6] + $upc[8] + $upc[10]; $oddPositions *= 3; $evenPositions= $upc[1] + $upc[3] + $upc[5] + $upc[7] + $upc[9]; $sumEvenOdd = $oddPositions + $evenPositions; $checkDigit = (10 - ($sumEvenOdd % 10)) % 10; } return $checkDigit; }

Here's a sample of a script (omitting the mysql connection code) that uses this function to validate UPC codes from a mysql database containing products_id (int), products_name (varchar[40]) and products_upc (varchar[12]).


<?php
$sql = "select products_id, products_name, products_upc from products\n";
$sql .= "where products_status = '1' and products_upc <> ''\n";
$sql .= "order by products_id";
$result = mysql_db_query($sql);
$count = 0;
echo "<html>\n";
echo "<head><title>Validate UPC Codes</title></head>\n";
echo "<body>\n<table border='0' cellpadding='2' cellspacing='0'>\n";
echo "<tr>\n";
echo '<td>Reference</td>';
echo '<td>Product Id</td>';
echo '<td>Product Name</td>';
echo '<td>UPC Code<br />w/o Check Digit</td>';
echo '<td>UPC Check Digit</td>';
echo '<td>Calculated<br />Check Digit</td>';
echo '<td>Result</td>';
echo "</tr>\n";
while ($row = mysql_db_fetch_assoc($result)) {
$count++; $check_digit = calculate_upc_check_digit($row['products_upc']);
$match = 'Ok';
if ($check_digit == -1) {
$match = '<strong>Failure</strong>';
} elseif ($check_digit != substr($row['products_upc'],11,1)) {
$match = '<strong>Error</strong>';
}
echo "<tr>\n";
echo '<td>' . $count . '</td>';
echo '<td>' . $row['products_id'] . '</td>';
echo '<td>' . tep_get_products_name($row['products_id']) . '</td>';
echo '<td>' . substr($row['products_upc'],0,11) . '</td>';
echo '<td>' . substr($row['products_upc'],11,1) . '</td>';
echo '<td>' . $check_digit . '</td>';
echo '<td>' . $match . '</td>';
echo "</tr>\n";
}
echo "</table>\n";
echo "</body>\n";
echo "</html>\n"
?>