Utility class to validate, format and generate NHS numbers.
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.
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:
- 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
You can install the package with Composer using the following command:
composer require imliam/php-nhs-number:^1.1
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']
composer test
Please see the changelog file for more information on what has changed recently.
Please see the upgrading file for details on upgrading from previous versions.
Please see the contributing file and code of conduct for details on contributing to the project.
If you discover any security related issues, please email liam@liamhammett.com instead of using the issue tracker.
- Liam Hammett
- Peter Fisher for the original class
- All Contributors
The MIT License (MIT). Please see the license file for more information.