Note: This post is over two years old and so the information contained here might be out of date. If you do spot something please leave a comment and we will endeavour to correct.

30th April 2009 - 5 minutes read time

EAN13 barcodes are commonly used to label products in Europe. If you want to know more about how they work then please view the Wikipedia entry on European Article Numbers.

EAN13 barcodes are actually 12 digits long and are validated by using a check digit, which is placed at the end, making the code 13 digits long. The check digit is worked out by the following process:

Add up all of the even numbers and multiply this number by 3.

Add up all of the odd numbers and add this result to the result of the even numbers.

Divide the number by 10 and keep the remainder (modulo).

If the remainder is not 0 then subtract 10 from this number.

Here is a function that runs through those steps, but also check to see what length the barcode is. If it is 13 digits long then it returns both the original check digit and the calculated check digit. If the barcode is 12 digits long then it returns the checksum. In both cases the original barcode is also returned.

function validateEan13($digits)
{
$originalcheck = false;
$digits = strval($digits);
if (strlen($digits) == 13) {
$originalcheck = substr($digits, -1);
} elseif (strlen($digits) != 12) {
// Invalid EAN13 barcode
return false;
}
$digits = str_split($digits);
// Add odd numbers together
$odd = $digits[1] + $digits[3] + $digits[5] + $digits[7] + $digits[9] + $digits[11];
// Multiply this result by 3
$odd = $odd * 3;
// Add even numbers together
$even = $digits[0] + $digits[2] + $digits[4] + $digits[6] + $digits[8] + $digits[10];
// Add two totals together
$total = $even + $odd;
// Calculate the checksum
// Divide total by 10 and store the remainder
$checksum = $total % 10;
// If result is not 0 then take away 10
if ($checksum != 0) {
$checksum = 10 - $checksum;
}
// Return results.
$digits = implode('', $digits);
if ($originalcheck !== false) {
return ['barcode' => $digits, 'checksum' => $checksum, 'originalcheck' => $originalcheck];
} else {
return ['barcode' => $digits, 'checksum' => $checksum];
}
}

To test this I ran a few codes through the function.

// two normal barcodes
print_r(validateEan13(5023920187205));
print_r(validateEan13(5010548001860));
// one short barcode to work out checksum
print_r(validateEan13(501054800186));
// a normal barcode
print_r(validateEan13(5034504935778));
// the same barcode with a broken number
print_r(validateEan13(5034504735778));
// two random numbers, one of which is not long enough to be an EA13 barcode
print_r(validateEan13(7233897438712));
var_dump(validateEan13(3345345345));

This prints out the following results, which I have annotated for your convenience.

// two normal barcodes
Array
(
[barcode] => 502392018720
[checksum] => 5
[originalcheck] => 5
)
Array
(
[barcode] => 501054800186
[checksum] => 0
[originalcheck] => 0
)
// one short barcode to work out checksum
Array
(
[barcode] => 501054800186
[checksum] => 0
)
// a normal barcode
Array
(
[barcode] => 503450493577
[checksum] => 8
[originalcheck] => 8
)
// the same barcode with a broken number
Array
(
[barcode] => 503450473577
[checksum] => 4
[originalcheck] => 8
)
// two random numbers, one of which is not long enough to be an EA13 barcode
Array
(
[barcode] => 723389743871
[checksum] => 4
[originalcheck] => 2
)
bool(false)

When I ran the code it did produce a few errors so I spent a little time cleaning it up. I've learnt a lot in 13 years and probably wouldn't write the code in the same way again. I dislike functions that return arrays of information when they say "validate".

Name

Philip Norton

Submitted by giHlZp8M8D on Mon, 06/13/2022 - 08:34

XML is a useful format for configuration, data storage, and transmitting data from one system to another. As a human readable format that can be easily read by machines it quickly gained favor in lots of different systems as a mechanism for data storage.

Pi day was a few weeks ago, but I came across this simple approximation of pi recently and decided to put together an example in PHP since it seemed pretty simple.

This approximation of pi centers around a real world example, but we can simulate this using some code.

A parabolic curve is a type of curve where every point is an equal distance from a focal point. There a number of different way to generate this sort of curve using math, but one of the simplest is to use straight lines to create the illusion of the curve.

I quite like the end of the year report from Spotify that they call "Wrapped". This is a little application in which they tell you what your favorite artist was and what sort of genres you listened to the most during the year.

Let's say you had a class that you wanted to use, but there was some sort of error in creating the object. This might be that the wrong parameters were passed, or the third party service (eg. a database) wasn't available at the time of creation.

## Comments

Hi, is it possible, that you switched odd and even numbers?

Submitted by Old one on Mon, 06/13/2022 - 07:26

PermalinkVery possible, yes.

When I ran the code it did produce a few errors so I spent a little time cleaning it up. I've learnt a lot in 13 years and probably wouldn't write the code in the same way again. I dislike functions that return arrays of information when they say "validate".

Submitted by giHlZp8M8D on Mon, 06/13/2022 - 08:34

Permalink## Add new comment