EAN13 is a barcode format. It consists of 12 numbers which you will generally have a range assigned to you. The 13th digit is called the check digit and is formulated by loooking at the other 12 digits. The purpose of the check digit is to ensure that the number is being read correctly as if any of the numbers do not match up, the check digit will not validate.
If you need to create check digits in PHP, here is my handy function:
function ean13_check_digit($digits){
//first change digits to a string so that we can access individual numbers
$digits =(string)$digits;
// 1. Add the values of the digits in the even-numbered positions: 2, 4, 6, etc.
$even_sum = $digits{1} + $digits{3} + $digits{5} + $digits{7} + $digits{9} + $digits{11};
// 2. Multiply this result by 3.
$even_sum_three = $even_sum * 3;
// 3. Add the values of the digits in the odd-numbered positions: 1, 3, 5, etc.
$odd_sum = $digits{0} + $digits{2} + $digits{4} + $digits{6} + $digits{8} + $digits{10};
// 4. Sum the results of steps 2 and 3.
$total_sum = $even_sum_three + $odd_sum;
// 5. The check character is the smallest number which, when added to the result in step 4, produces a multiple of 10.
$next_ten = (ceil($total_sum/10))*10;
$check_digit = $next_ten - $total_sum;
return $digits . $check_digit;
}
Other Barcode Related Resources:
http://phpclasses.fonant.com/browse/package/3643.html
http://thinkabdul.com/2007/01/04/barcoder-freeware-ean-13-barcode-reader-for-java-j2me-mobiles/

There is a better way that works with EAN13 and EAN8:
function get_ean_checkdigit($barcode){ $sum = 0; for($i=(strlen($barcode));$i>0;$i--){ $sum += (($i % 2) * 2 + 1 ) * substr($barcode,$i-1,1); } return (10 - ($sum % 10)); }–
Rémi
nice – added syntax highlighting to make it more readable
Function added by Rémi is not correct, it gives out the wrong result.
However, original function by admin is correct.
Thanks a lot.
Function added by ADMIN is not correct – you need to multiple odd number by 3 not even
$next_ten = (ceil(((3*$odd_sum)+$even_sum)/10))*10
ONLY THANKS!
function isValidEAN($n) {
// valid ean returns 1
$check = 0;
for ($i = 0; $i < 13; $i+=2) $check += substr($n, $i, 1);
for ($i = 1; $i < 12; $i+=2) $check += 3 * substr($n, $i, 1);
return $check % 10 == 0;
}
function validate_ean($barcode){
if (strlen($barcode) == 8) { $barcode = ’00000′ . $barcode; } #patch EAN8 to EAN13
if (strlen($barcode) != 13){ return false; }
$sum = 0;
for($i=0; $i<(strlen($barcode)-1); $i++){
$sum += (($i % 2) * 2 + 1 ) * substr($barcode,$i,1);
}
$checksum = (10 – ($sum % 10));
if ($checksum != substr($barcode,-1)){
return(false);
}
return(true);
}
Please note if copying “another version” code, replace “–” with hyphen “-”, or it will not work!!! Took me a few minutes to debug that… Otherwise, seems to work well.
The code is correct thx, but the comments and name of variables are mixed. (Even and odd numbers)
Hello,
This code is very pretty and efficient.
I use it for my modules.
Thanks a lot.
I improved it to handle EAN and UPC :
[quote]
public static function EAN_UPC_Check($code){
//first change digits to a string so that we can access individual numbers
$digits = sprintf(‘%012s’, substr(sprintf(‘%013s’, $code), 0, 12)) ;
// 1. Add the values of the digits in the even-numbered positions: 2, 4, 6, etc.
$even_sum = $digits{1} + $digits{3} + $digits{5} + $digits{7} + $digits{9} + $digits{11};
// 2. Multiply this result by 3.
$even_sum_three = $even_sum * 3;
// 3. Add the values of the digits in the odd-numbered positions: 1, 3, 5, etc.
$odd_sum = $digits{0} + $digits{2} + $digits{4} + $digits{6} + $digits{8} + $digits{10};
// 4. Sum the results of steps 2 and 3.
$total_sum = $even_sum_three + $odd_sum;
// 5. The check character is the smallest number which, when added to the result in step 4, produces a multiple of 10.
$next_ten = (ceil($total_sum/10))*10;
$check_digit = $next_ten – $total_sum;
return ((int)$code == (int)($digits . $check_digit));
}
[/quote]
Olivier.