【问题标题】:PHP: counter variable in recursive function counts down before returning "1"PHP:递归函数中的计数器变量在返回“1”之前倒计时
【发布时间】:2018-12-30 22:50:45
【问题描述】:

我有以下问题: 递归的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

【问题讨论】:

  • 正确缩进代码使其更具可读性...
  • 想象对counterFunction($counter) 的内部调用正在调用任何其他函数,因为从内部调用该函数并不重要。这可以完美地解释这种行为,不是吗?
  • 有什么问题?一切正常吗?
  • 您的问题不是问题。你能解释一下你期望你的输出是什么样的吗?
  • @devon:谢谢你的提示!

标签: php recursion return


【解决方案1】:

完全按照您的指示进行

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
}

这可能会产生不希望的效果,即您将获得多个带有“结束条件”的打印,而不是实际的基本情况,因为每次调用都会这样做。

【讨论】:

    【解决方案2】:

    我不知道你的最终目标是什么。对我来说,这个功能目前似乎没用。

    这是我返回 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;
      }
    }
    

    【讨论】:

      【解决方案3】:

      你不保存返回的值。

      换行

      counterFunction($counter);
      

      $counter=counterFunction($counter);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-11
        • 1970-01-01
        • 1970-01-01
        • 2014-04-27
        • 1970-01-01
        相关资源
        最近更新 更多