gpt4 book ai didi

php - 为什么我的函数不返回返回值?

转载 作者:行者123 更新时间:2023-11-29 01:13:58 25 4
gpt4 key购买 nike

好的,这是我的功能:

function getNewJobNumber($jobPrefix, $addition = "0") {
$addition = $addition + 1;
//echo $addition . "<br />";
$yearDate = date("Y");
$firstDigit = $yearDate[strlen($yearDate) - 1];
$db = DatabaseHelpers::getDatabaseConnection();
$jobQuery = 'SELECT jobID, jobNumber, jobPrefix FROM tblJobNumbers WHERE jobPrefix = "' . $jobPrefix . '" AND jobNumber LIKE "' . $firstDigit . '___" ORDER BY jobID DESC LIMIT 1';
//echo $jobQuery . "<br />";
$stmt1 = $db->query($jobQuery);
$stmt1->setFetchMode(PDO::FETCH_OBJ);
$firstResult = $stmt1->fetch();
//above should select the latest created job number with selected prefix
//print_r($firstResult);
$jobNumber = $firstResult->jobNumber; //top row, will be last job number
//echo "jobNumberFromDB:" . $jobNumber . "<br />";
if (!$jobNumber) {
//no job number exists yet, create one
//will be last digit of year followed by "000" ie in 2013 first
//new job number is "3000"
$newJobNumber = str_pad($firstDigit, 4, "0");
return $newJobNumber;
} else {
//job number already exists, try next one
$nextJobNumber = $jobNumber + $addition;
$nextJobQuery = 'SELECT jobID, jobNumber, jobPrefix FROM tblJobNumbers WHERE jobPrefix = "' . $jobPrefix . '" AND jobNumber = "' . $nextJobNumber . '" ORDER BY jobID DESC LIMIT 1';
$stmt2 = $db->query($nextJobQuery);
$stmt2->setFetchMode(PDO::FETCH_OBJ);
$nextResult = $stmt2->fetch();
$dbNextJobNumber = $nextResult->jobNumber;
if (!$dbNextJobNumber) {
//new job number is unique, return value
echo "return:nextJobNumber-" . $nextJobNumber . "<br />";
return($nextJobNumber);
} else {
//new job number is not unique, and therefore we need another one
if ($addition <= 99) { //don't let this recurse more than 99 times, it should never need to
//in order to loop this programatically call function again, adding one to addition factor
getNewJobNumber($jobPrefix, $addition+1);
} else {
return;
}
}
}
}

这是我的电话:

        $ourNewJobNumber = getNewJobNumber($_POST['txtJobPrefix'], 0);
echo ":}" . $ourNewJobNumber . "{:<br />";

这是我的结果:

return:nextJobNumber-3005
:}{:

代码执行完美,从数据库中提取值并比较它们,并按照我想要的方式执行所有操作。它在我可以测试的每种情况下都获得了正确的值,但它完全拒绝将该值返回给调用脚本。有没有人看到我掩盖的任何愚蠢错误?在我的 return 语句之前立即进行调试回显似乎消除了它在 return 语句之前出错的任何可能性,但我现在还不知道。

编辑:需要说明的是,此时 3005 是我期望从我的数据库中获取的值。这是为了在工作中设置工作编号,该编号始终为 Zxxx,其中 Z 是年份的最后一位。这些总是按顺序创建的,但对于跨度超过一年的作业,我们只更改 Z,所以这是我用来解决 3030 可以(并且确实)在创建 3000 之前存在这一事实的代码。

最佳答案

你正在递归地调用你的函数,但你没有对返回值做任何事情:

    if ($addition <= 99) { //don't let this recurse more than 99 times, it should never need to
//in order to loop this programatically call function again, adding one to addition factor
getNewJobNumber($jobPrefix, $addition+1);
} else {
return;
}

应该是这样的:

    if ($addition <= 99) { //don't let this recurse more than 99 times, it should never need to
//in order to loop this programatically call function again, adding one to addition factor
return getNewJobNumber($jobPrefix, $addition+1);
^^^^^^
} else {
return -1; // some kind of error message?
}

关于php - 为什么我的函数不返回返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14323943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com