Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

returning the value of an if statement within a PHP function

I’m trying to write a function that returns a random number for every new student registered, but each time the form is submitted the function returns a null value, and the DB does not accept null values, it ought to return something like this: DFA/SSS/22/1246 here’s the code:

function createRegNumber()
{
    $schname = "DFA";
    $month = date("m");
    $year = date("Y");
    $new_year = substr($year, 2, 2);

    $base_year = 2019; // Set a base when the intakes started

    $intake = intval($year) - $base_year; // This will increase for every year

    $increase_with = $intake++;


    if ($month == '3') {
        $intake += $increase_with;
        $reg_no = $schname . "/" . $_POST['category'] . "/" . $new_year . "/" . rand(1000, 9999);
    } else if ($month == '9') {
        $increase_with++;
        $intake += $increase_with;
        $reg_no = $schname . "/" . $_POST['category'] . "/" . $new_year . "/" . rand(1000, 9999);

    }
    return $reg_no;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Looks like the issue is that you return $reg_no but unless $month == '3' or $month == '9' it never gets set. Most likely you want:

if ($month == '3') {
    $intake += $increase_with;
} else if ($month == '9') {
    $increase_with++;
    $intake += $increase_with;
}
$reg_no = $schname . "/" . $_POST['category'] . "/" . $new_year . "/" . rand(1000, 9999);
return $reg_no;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading