PHPackages                             imliam/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. imliam/php-nhs-number

ActiveLibrary

imliam/php-nhs-number
=====================

Utility class to validate, format and generate NHS numbers.

v1.1.0(6y ago)213.2k↓100%3[3 PRs](https://github.com/ImLiam/php-nhs-number/pulls)MITPHPPHP ^7.1

Since Jul 23Pushed 1y ago3 watchersCompare

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

READMEChangelog (2)Dependencies (1)Versions (4)Used By (0)

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

36

—

LowBetter than 82% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 63.6% of commits — single point of failure

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 ~365 days

Total

2

Last Release

2487d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3e8a14b9f997cf85aacea7d39da9dc33c38cc05fe03360578327ea9bcb25f4d9?d=identicon)[ImLiam](/maintainers/ImLiam)

---

Top Contributors

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

---

Tags

imliamphp-nhs-number

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[imliam/laravel-env-set-command

Set a .env file variable from the command line

118352.4k10](/packages/imliam-laravel-env-set-command)[imliam/laravel-blade-helper

An easier way to define custom Blade directives.

185121.0k](/packages/imliam-laravel-blade-helper)[imliam/laravel-throttle-simultaneous-requests

Throttle the current user's requests based on how many requests are currently being executed.

4623.0k](/packages/imliam-laravel-throttle-simultaneous-requests)[imliam/php-unique-gmail-address

Ensure that a Gmail address is unique

914.9k](/packages/imliam-php-unique-gmail-address)

PHPackages © 2026

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