PHP:递归函数中的计数器变量在返回“ 1”之前递减计数

时间:2018-07-23 13:05:33

标签: php recursion return

我有以下问题: 递归counterFunction不返回最后一个值,而是倒数我的$counter变量,然后在每种情况下都返回1。 不幸的是,当我在ifelse中都放入退货时,它也不起作用。

对此,我深表感谢。预先感谢。

$counterReceive = counterFunction(0);
echo "CounterReceive: ".$counterReceive."</br>";

function counterFunction($counter)
{
    if ($counter < 3) {
        $counter++;
        echo "counter in recursive loop: ".$counter."</br>";
        counterFunction($counter);
    }
    echo "end condition reached.</br>";
    echo "End Counter: ".$counter."</br>";
    return $counter;
}

输出:

counter in recursive loop: 1  
counter in recursive loop: 2  
counter in recursive loop: 3  
end condition reached.  
End Counter: 3  
end condition reached.  
End Counter: 2  
end condition reached.  
End Counter: 1  
CounterReceive: 1  

Photo of my Output that describes the problem in a nutshell

3 个答案:

答案 0 :(得分:0)

您不保存返回的值。

换行

counterFunction($counter);

$counter=counterFunction($counter);

答案 1 :(得分:0)

我不知道你的最终目标是什么。在我看来,此功能目前无用。

这是我返回10的方法:-)

https://ideone.com/jorZID

function counterFunction($counter){
  if($counter < 10){
    $counter++;
    echo "counter in recursive loop: ".$counter."</br>";
    return counterFunction($counter);
  } else {
    return $counter;
  }
}

答案 2 :(得分:0)

它确实完全按照您的要求

function counterFunction(int $counter) :int
{
    if ($counter < 3) {
        $counter++;                                            // mutate $counter to be increase by 1
        echo "counter in recursive loop: ".$counter."</br>";   // print info with new $counter binding
        counterFunction($counter);                             // call counterFunction AND DISCARD the result (it is not assigned or returned)
    }
    // This happens for each call regardless if it does recursion or not
    echo "end condition reached.</br>";                        // print info
    echo "End Counter: ".$counter."</br>";                     // print End counter for every stage or recursion, which is argument increased if argument was below 3
    return $counter;                                           // return argument increased by 1 regardless of possible recursion
}

您可能认为两次counterFunction的呼叫共享$counter,但事实并非如此。他们有自己的参数副本,除非它是对象或通过引用传递。在这种情况下不是。

要修复它,您应该使用递归的结果。例如。

function counterFunction(int $counter) :int
{
    if ($counter < 3) {
        echo "counter in recursive loop: {$counter}</br>";     // print info with current $counter binding (argument)
        return counterFunction($counter+1);                    // call counterFunction with $counter+1 as argument and returne the result
    }
    // only happens when $counter >= 3. returns argument
    echo "end condition reached.</br>";                        // print info
    echo "End Counter: {$counter}</br>";                       // print End counter, which is the same as argument
    return $counter;                                           // return argument
}

或者您可以使用结果更新计数器:

function counterFunction(int $counter) :int
{
    if ($counter < 3) {
        echo "counter in recursive loop: {$counter}</br>";     // print info with current $counter binding (argument)
        $counter = counterFunction($counter+1);                // call counterFunction with $counter+1 as argument and update $counter
    }
    // This happens for each call regardless if it does recursion or not
    echo "end condition reached.</br>";                        // print info
    echo "End Counter: {$counter}</br>";                       // print End counter, which is the same as argument
    return $counter;                                           // return argument
}

这可能会带来不必要的影响,因为与每次调用相比,您将多次打印带有“结束条件”的印刷品,而不是实际的基本情况。