PHPackages                             fredbradley/php-nhs-number - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. fredbradley/php-nhs-number

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

fredbradley/php-nhs-number
==========================

Utility class to validate, format and generate NHS numbers.

2.0.0(4y ago)03.0k↓50%11MITPHPPHP ^8.0

Since Jul 23Pushed 4y agoCompare

[ Source](https://github.com/fredbradley/php-nhs-number)[ Packagist](https://packagist.org/packages/fredbradley/php-nhs-number)[ Docs](https://github.com/imliam/php-nhs-number)[ RSS](/packages/fredbradley-php-nhs-number/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (5)Used By (1)

NHS Numbers
===========

[](#nhs-numbers)

[![Latest Version on Packagist](https://camo.githubusercontent.com/655cb1ef9ce819766446d1e04ef080ecc422c593d9400ef89e56b61d38230e40/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696d6c69616d2f7068702d6e68732d6e756d6265722e737667)](https://packagist.org/packages/imliam/php-nhs-number)[![Build Status](https://camo.githubusercontent.com/25abe2e3e52a8797ba63fe0c37c725bb4dd7c0132197ba1d814aeddabcf05837/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f696d6c69616d2f7068702d6e68732d6e756d6265722e737667)](https://travis-ci.org/imliam/php-nhs-number)[![Code Quality](https://camo.githubusercontent.com/2d1530f2100d588a63ea1f843774f87f5279453a667fde7648c8a3c7460d5a8f/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f696d6c69616d2f7068702d6e68732d6e756d6265722e737667)](https://camo.githubusercontent.com/2d1530f2100d588a63ea1f843774f87f5279453a667fde7648c8a3c7460d5a8f/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f696d6c69616d2f7068702d6e68732d6e756d6265722e737667)[![Code Coverage](https://camo.githubusercontent.com/60eceaa080861a200a0d06d1ef62bbbc84fe494fd8f43df9c7e20c091d8a2fa2/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f696d6c69616d2f7068702d6e68732d6e756d6265722e737667)](https://camo.githubusercontent.com/60eceaa080861a200a0d06d1ef62bbbc84fe494fd8f43df9c7e20c091d8a2fa2/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f696d6c69616d2f7068702d6e68732d6e756d6265722e737667)[![Total Downloads](https://camo.githubusercontent.com/9b030a757d771ad0d5a0836c9536d82b0e616c3fcbe7b21b595fa5172b39b36d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696d6c69616d2f7068702d6e68732d6e756d6265722e737667)](https://packagist.org/packages/imliam/php-nhs-number)[![License](https://camo.githubusercontent.com/7968b4c142c3a4581fcd6b54ffdee652479f9c607fbd09ff0f99160ce6962151/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f696d6c69616d2f7068702d6e68732d6e756d6265722e737667)](LICENSE.md)

Utility class to validate, format and generate NHS numbers.

- [NHS Numbers](#nhs-numbers)
    - [🤔 F.A.Q.](#%F0%9F%A4%94-faq)
        - [What is an NHS number?](#what-is-an-nhs-number)
        - [How do you validate an NHS number?](#how-do-you-validate-an-nhs-number)
    - [💾 Installation](#%F0%9F%92%BE-installation)
    - [📝 Usage](#%F0%9F%93%9D-usage)
    - [✅ Testing](#%E2%9C%85-testing)
    - [🔖 Changelog](#%F0%9F%94%96-changelog)
    - [⬆️ Upgrading](#%E2%AC%86%EF%B8%8F-upgrading)
    - [🎉 Contributing](#%F0%9F%8E%89-contributing)
        - [🔒 Security](#%F0%9F%94%92-security)
    - [👷 Credits](#%F0%9F%91%B7-credits)
    - [♻️ License](#%E2%99%BB%EF%B8%8F-license)

🤔 F.A.Q.
--------

[](#-faq)

### What is an NHS number?

[](#what-is-an-nhs-number)

An "NHS number" is a unique identifier that every individual patient registered with Great Britain's National Health Service (NHS) has.

[You can find out more about NHS numbers on the nhs.uk website.](https://www.nhs.uk/NHSEngland/thenhs/records/nhs-number/Pages/what-is-the-nhs-number.aspx)

### How do you validate an NHS number?

[](#how-do-you-validate-an-nhs-number)

Not every number is a valid NHS number - they must conform to a simple algorithm to be considered valid. Before the algorithm is run, there are a few things to note about NHS numbers:

- An NHS number must be numeric
- An NHS number must be 10 digits long
- An NHS number can begin with a `0`, so it should be handled as a string, not an integer
- The last digit of the NHS number is used as the "check digit" for the algorithm

The algorithm to validate an NHS number using its "check digit" is as follows:

1. Multiple each of the first nine digits by a defined weight, shown below:

Original digitMultiplied by11029384756657483922. Calculate the sum of all 9 multiplications
3. Divide this sum by 11 and get the remainder
4. Subtract 11 from the remainder to get the total
5. If the total is 11 then the identifier, otherwise the identifier is the total
6. If the identifier is 10, then the NHS number is wrong
7. If the identifier is the same as the check digit, then the NHS number is correct

💾 Installation
--------------

[](#-installation)

You can install the package with [Composer](https://getcomposer.org/) using the following command:

```
composer require imliam/php-nhs-number:^1.1
```

📝 Usage
-------

[](#-usage)

You can new up the `NhsNumber` object by passing through an NHS number.

```
$nhsNumber = new \ImLiam\NhsNumber\NhsNumber('9077844449');
```

The NHS recommend that when displaying an NHS number to a human, it should be spaced out in a 3-3-4 format, which helps it to be easier to read. To do this, call the `->format()` method or cast the object to a string.

```
echo $nhsNumber->format();
// '907 784 4449'

echo (string) $nhsNumber;
// '907 784 4449'
```

To get a boolean representation of whether or not the current number is valid, call the `->isValid()` method on the object.

```
$nhsNumber->isValid();
// true
```

If you need more information on why a given NHS number may be invalid, you can call the `->validate()` method directly. This will throw an `InvalidNhsNumberException` exception whose message will explain why the number is not valid.

```
try {
    $nhsNumber = new \ImLiam\NhsNumber\NhsNumber('9077844446');
    $nhsNumber->validate();
} catch (\ImLiam\NhsNumber\InvalidNhsNumberException $e) {
    echo $e->getMessage();
    // "The NHS number's check digit does not match."
}
```

To generate a single or multiple random valid NHS numbers for testing purposes, call the `::getRandomNumber()` or `::getRandomNumbers($count)` static methods respectively.

```
echo \ImLiam\NhsNumber\NhsNumber::getRandomNumber();
// '9278462608'

echo \ImLiam\NhsNumber\NhsNumber::getRandomNumbers(5);
// ['7448556886', '0372104223', '8416367035']
```

✅ Testing
---------

[](#-testing)

```
composer test
```

🔖 Changelog
-----------

[](#-changelog)

Please see [the changelog file](CHANGELOG.md) for more information on what has changed recently.

⬆️ Upgrading
------------

[](#️-upgrading)

Please see the [upgrading file](UPGRADING.md) for details on upgrading from previous versions.

🎉 Contributing
--------------

[](#-contributing)

Please see the [contributing file](CONTRIBUTING.md) and [code of conduct](CODE_OF_CONDUCT.md) for details on contributing to the project.

### 🔒 Security

[](#-security)

If you discover any security related issues, please email  instead of using the issue tracker.

👷 Credits
---------

[](#-credits)

- [Liam Hammett](https://github.com/imliam)
- [Peter Fisher](https://github.com/pfwd/NHSNumber-Validation) for the original class
- [All Contributors](../../contributors)

♻️ License
----------

[](#️-license)

The MIT License (MIT). Please see the [license file](LICENSE.md) for more information.

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~582 days

Total

3

Last Release

1684d ago

Major Versions

v1.1.0 → 2.0.02021-09-30

PHP version history (2 changes)v1.0.0PHP ^7.1

2.0.0PHP ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1639226?v=4)[Fred Bradley](/maintainers/fredbradley)[@fredbradley](https://github.com/fredbradley)

---

Top Contributors

[![fredbradley](https://avatars.githubusercontent.com/u/1639226?v=4)](https://github.com/fredbradley "fredbradley (7 commits)")[![imliam](https://avatars.githubusercontent.com/u/4326337?v=4)](https://github.com/imliam "imliam (6 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (3 commits)")

---

Tags

imliamphp-nhs-number

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/fredbradley-php-nhs-number/health.svg)

```
[![Health](https://phpackages.com/badges/fredbradley-php-nhs-number/health.svg)](https://phpackages.com/packages/fredbradley-php-nhs-number)
```

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
