Home > front end >  returning the value of an if statement within a PHP function
returning the value of an if statement within a PHP function

Time:05-12

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;
}

CodePudding user response:

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;
  • Related