My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

How to create Exam/Registration Number or pin in php/laravel

Chidiebere Chukwudi's photo
Chidiebere Chukwudi
·Nov 19, 2021·

1 min read

To create an exam Number pin in php/laravel. You can achieve that using this approach

function reg_number($id)
        {
            $regNum = '';
            $uniqueId = str_pad($id, 4, '0', STR_PAD_LEFT);
            $date = date('y');
            $regNum = "SCH" . '\\' . $date . '\\' . $uniqueId;
            return $regNum;
        };

The function accepts a single parameter.

The function starts with an initialised $regum variable followed by a variable $uniqueId where we use str_pad() php native function that takes the first parameter which can be any positive integer, followed by a digit, for example, 4 that determines the number of zeros which is the 3rd parameter, '0' then the zeros (0s) are appended to the left by STR_PAD_LEFT

A typical result is as follows:

 echo str_pad(6, 4, '0', STR_PAD_LEFT);

// 0006

Next, we will need to get the last two digits in a typical calendar year with:

$date = date(y);
echo $date;
// 21

Then actual variable, $regNum, is where we concatenate the school name, say Sch followed by a backslash then year as $date and lastly the unique id number as $uniqueId then we return return $regNum; Typical result is :

 echo reg_number(6);
// SCH\21\0006