DEV Community

Chidiebere Chukwudi
Chidiebere Chukwudi

Posted on • Edited on

3

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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (2)

Collapse
 
dozykeys profile image
Duru Chidozie

Nice

Collapse
 
jovialcore profile image
Chidiebere Chukwudi

Thank you so much

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay