---
slug: "jan-checkdigit-validation-php-code"
title: "Code to Validate Check Digits of JAN Codes in PHP"
description: "Here is an overview in English of the Japanese blog article:\n\n\"The blog article introduces a PHP function that checks if a given JAN code is valid. If the JAN code is valid, the function returns true; if it is invalid, it returns false and an error message.\""
url: "https://www.ytyng.com/en/blog/jan-checkdigit-validation-php-code"
publish_date: "2023-04-01T02:27:15Z"
created: "2023-04-01T02:27:15Z"
updated: "2026-02-27T12:05:16.893Z"
categories: ["PHP"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20250708/edb8f91437a8434b8c3f864258ae01c3.png.webp?width=768"
has_video: true
has_music: true
video_urls: ["https://media.ytyng.net/ytyng-blog/278/featured-video-1.mp4", "https://media.ytyng.net/ytyng-blog/278/featured-video-2.mp4", "https://media.ytyng.net/ytyng-blog/278/featured-video-3.mp4"]
music_urls: ["https://media.ytyng.net/ytyng-blog/278/featured-music-278-3.mp3", "https://media.ytyng.net/ytyng-blog/278/featured-music-278-4.mp3"]
lang: "en"
---

# Code to Validate Check Digits of JAN Codes in PHP

```php
/**
 * Validate the JAN check digit and return [success?, message]
 */
function validate_jan($jan): array
{
    $match = preg_match('|^(\d{12})(\d)$|', $jan, $matches);
    if (!$match) {
        return [false, 'Digit count error'];
    }
    $chars = str_split($matches[1]);
    $odd_total = 0;
    $even_total = 0;
    foreach ($chars as $i => $v) {
        if ($i % 2 == 0) {
            // Starting from 0, so 0 is an odd digit
            $odd_total += $v;
        } else {
            $even_total += $v;
        }
    }
    $total = $even_total * 3 + $odd_total;
    $digit = (10 - ($total % 10)) % 10;
    if ($matches[2] != $digit) {
        return [false, "Check digit mismatch. The correct digit is {$digit}"];
    }
    return [true, ""];
}
```
