Barcodes (often written as bar codes) are optical, machine-readable, representations of data; this data is usually related to the object that carries the barcode (product name, description, price, location, etc).
Barcodes are used in almost every business and manufacturing environment. They form the basis of the vast majority of automatic data capture operations. In the same sense, validating barcode numbers allows companies to avoid misplaced shipments, costly repackaging and, in some circumstances, wrongly processing products, rendering them unusable.
The mapping between messages and barcodes is called a symbology. The specification of a symbology includes the encoding of the message into bars and spaces, any required start and stop markers, the size of the quiet zone required to be before and after the barcode, and the computation of a checksum. Bar Code Symbologies differ in qualities such as capacity and linearity, even ability to withstand damage without losing information.
The barcode technology never stops advancing. For instance, the recent surge of 2D barcodes has proven popular by allowing you to scan straight from your smartphone for a wealth of data. Below is a list of the most commonly used barcodes.
Later, two-dimensional (2D) variants were developed, using rectangles, dots, hexagons and other geometric patterns, called matrix codes, although they do not use bars as such. One of the best known 2D matrix code is the QR code.
Once barcodes are scanned, it’s necessary to validate whether the scanned data is well formed and whether the validation data (usually, check digits) are valid. In this sense, validation of a barcode means making sure the barcode represents the product it is intended to represent—or that it refers to any product at all.
As mentioned above, validating barcodes can be performed by checking symbol count and, in some cases, checksum digits (using somewhat complex math operations). While this might be a feasible option for small amounts of barcodes, doing this for many codes requires too much time and goes against the very essence of using barcodes to speed and automate the delivery of products.
Using PHP to validate barcode numbers
A few months ago, I worked on a project that involving different types of linear barcodes. After scanning the codes, validation occurred using a set of functions, one for each type of code. However, some codes share common calculations, such as calculating check digits. Even more, some barcodes are variations of each other and validation functions could be consolidated into a single function
With this in mind, I created a new PHP class, called BarcodeValidator, consolidating these barcode validation functions into a set of static methods. This allows validating barcodes by simply calling the class’s methods (one for each type of code), without having to create an instance of the class.
The class also supports Composer based installs, making it simple for developers to integrate these validation functions into their projects. Moreover, method invocation only involves passing the code to validate and, in return, they return a boolean value indicating whether the code is valid or not.
Let’s say we have an ISBN code and we want to validate it. With this class, we only need to invoke it as follows:
Supported Barcode Validation types
Universal Product Codes (UPC)
UPC barcodes are used to label and scan consumer goods at points-of-sale around the world—mainly in the United States, but also in the United Kingdom, Australia, New Zealand and other countries. The UPC-A variation encodes 12 numerical digits while UPC-E is a smaller variation, which encodes only 6 numerical digits.
European Article Number codes (EAN)
EAN barcodes are also used to label consumer goods worldwide for point-of-sale scanning, primarily in Europe. They look very similar to UPC codes, and the main distinction is their geographical application. While EAN-13 (comprising 13 digits) is the default form factor, you’ll find EAN-8 (covering 8 digits) barcodes on products where only limited space is available, like small candies.
International Standard Book Number (ISBN)
ISBN barcodes are based on the EAN symbology. The ISBN numbering system predates barcoding and was incorporated into the EAN system by the use of a “978” prefix. From January 2007, ISBN numbers became 13 digits long, rather than the previous 10 digits.The change was necessary so that the ISBN system have sufficient numbers available for future growth.
Previously, the ISBN number was always prefixed with the digits “978” when applying the number to an EAN barcode. There will now be an added alternative (“979”) prefix in order to expand the numbering system. This means that the full 13 digits must now be communicated. The change also required that the 13-digit number should appear above the barcode.
International Mobile Equipment Identity (IMEI) codes
The IMEI (International Mobile Equipment Identity) is a unique number to identify GSM, WCDMA, and iDEN mobile phones, as well as some satellite phones. Mostly phone have one IMEI number, but in dual SIM phones are two. IMEI numbers either come in a 17 digit or 15 digit sequences of numbers.
Global Shipment Identification Number (GSIN)
The Global Shipment Identification Number (GSIN) is a globally unique number which is used to identify a grouping of logistic units which are part of the same shipment. The logistic units keep the same GSIN during all transport stages, from origin to final destination. GSIN also meets the requirements for UCR (Unique Consignment Reference) according to the World Customs Organisation, WCO.
Serial Shipping Container Code (SSCC)
An SSCC (Serial Shipping Container Code) is used to provide a number logistic units. An SSCC enables manufacturers, suppliers, carriers and buyers to track a logistic unit from production to end customer. SSCCs can be used for tracing goods, warehouse management and efficient handling in transport.
The same SSCC may not be used on two different logistic units, neither during transport or when they are handled in the warehouse of the transport buyer or goods recipient. This means that the life span of an SSCC, that is the period until the unit is unpacked or repacked, can be from a few to many years.
Global Location Number (GLN)
A GLN is used to uniquely identify a company or organization. A GLN can also be used to number delivery places, invoicing addresses, workplaces, branches as well as functions or roles, such as goods recipient or authorized purchaser.