DEV Community

Chidiebere Chukwudi
Chidiebere Chukwudi

Posted on • Updated on

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

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;

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

$date = date(y);
echo $date;
// 21 
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

Top comments (2)

dozykeys profile image
Duru Chidozie


jovialcore profile image
Chidiebere Chukwudi

Thank you so much