在 PHP 函数中返回 if 语句的值

returning the value of an if statement within a PHP function

我正在尝试编写一个函数,该函数 return 为每个注册的新学生提供一个随机数,但每次提交表单时,函数 return 都是一个空值,而数据库确实如此不接受空值,它应该 return 像这样:DFA/SSS/22/1246 这里是代码:

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

看起来问题是您 return $reg_no 但除非 $month == '3'$month == '9' 它永远不会设置。您很可能想要:

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;