NHS Numbers
, (*1)
Utility class to validate, format and generate NHS numbers., (*2)
๐ค F.A.Q.
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., (*3)
You can find out more about NHS numbers on the nhs.uk website., (*4)
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:, (*5)
- 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:, (*6)
- Multiple each of the first nine digits by a defined weight, shown below:
Original digit |
Multiplied by |
1 |
10 |
2 |
9 |
3 |
8 |
4 |
7 |
5 |
6 |
6 |
5 |
7 |
4 |
8 |
3 |
9 |
2 |
- Calculate the sum of all 9 multiplications
- Divide this sum by 11 and get the remainder
- Subtract 11 from the remainder to get the total
- If the total is 11 then the identifier, otherwise the identifier is the total
- If the identifier is 10, then the NHS number is wrong
- If the identifier is the same as the check digit, then the NHS number is correct
๐พ Installation
You can install the package with Composer using the following command:, (*7)
composer require imliam/php-nhs-number:^1.1
๐ Usage
You can new up the NhsNumber
object by passing through an NHS number., (*8)
``` php
$nhsNumber = new \ImLiam\NhsNumber\NhsNumber('9077844449');, (*9)
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.
```php
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., (*10)
$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., (*11)
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., (*12)
echo \ImLiam\NhsNumber\NhsNumber::getRandomNumber();
// '9278462608'
echo \ImLiam\NhsNumber\NhsNumber::getRandomNumbers(5);
// ['7448556886', '0372104223', '8416367035']
โ
Testing
bash
composer test
, (*13)
๐ Changelog
Please see the changelog file for more information on what has changed recently., (*14)
โฌ๏ธ Upgrading
Please see the upgrading file for details on upgrading from previous versions., (*15)
๐ Contributing
Please see the contributing file and code of conduct for details on contributing to the project., (*16)
๐ Security
If you discover any security related issues, please email liam@liamhammett.com instead of using the issue tracker., (*17)
๐ท Credits
โป๏ธ License
The MIT License (MIT). Please see the license file for more information., (*18)