search

Home  >  Q&A  >  body text

php function, the result is unexpected

<?php
function fun($i) {
    $i++;
    echo $i;
    if ($i<5) {
        fun($i);
    }
    echo $i;
}
fun(1);
我以为结果会是:23455,实际上是23455432,请问这个432是什么情况?
黄舟黄舟2821 days ago826

reply all(7)I'll reply

  • PHPz

    PHPz2017-05-24 11:36:32

    Understand recursion by considering the implementation function of the function without considering the callback process.
    If you consider the callback process to understand recursion, you must analyze the parameters and variables in the entire recursive process, and remember when they are called It seems a bit troublesome to call a function and return to the main calling function.
    The functions of some commonly used recursive functions are well described, such as递归求解阶乘:

    <?php
    
    function factorial($n) {
        if ($n != 1)
            $res = $n * factorial($n-1);
        else
            $res = 1;
        return $res;
    }
    
    ?>

    The function function is: 计算 n 的阶乘, 函数功能实现就是: n 的阶乘 = n * (n-1)的阶乘. If you understand it this way, you don’t need to consider the function call problem during the recursive process.

    For the function in question:
    The function function is: 输入 n,输出 n+1, 如果 n+1<5,执行 fun(n+1),再输出 n+1(It’s hard to describe in a short sentence):
    So according to the function description, you can get the execution process of the function, and finally get the result.

    1. Execute the output result of fun(1): 输入 1, 输出 2, 2+1<5,执行 fun(2),得到 fun(2) and then output 2.
      Final output: 2[output(fun(2))]2

    2. Similarly, fun(2)Output result: 3[Output(fun(3))]3
      Final output: 23[Output(fun(3))]32

    3. Similarly, fun(3)Output result: 4[Output(fun(4))]4
      Final output: 234[Output(fun(4))] 432

    4. Executionfun(4): 由于 4+1<5 判断失败, 不执行 fun(5), output result: 55
      Final output: 23455432

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-24 11:36:32

    When returning recursively, this sentence will be output backwards

    reply
    0
  • 为情所困

    为情所困2017-05-24 11:36:32

    The function is not async
    so you execute fun(1)
    2 $i++; echo $i;
    3 callback func(2) $i++; echo $i;
    4 callback func(3) $i++; echo $i;
    5 Callback func(4) $i++; echo $i;
    5 Callback func(4) Because after $i++, it is already greater than 5, so the following echo $i (the bottom echo $i of fun(4)) is executed. fun(4) the entire function execution ends)
    4 Since the execution of func(4) is completed, fun(3) will continue to be executed. The bottom echo $i
    3 2 also has the same logic

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-24 11:36:32

    When the fun function is recursive, when it is judged that $i < 5 is satisfied, it will call itself and push it onto the stack. At the same time, the last sentence of the function, echo $i, is not executed; when the recursive call reaches $i = 5; at this time Start returning to the previous stack point of the function and continue execution. That is, execute the last sentence in the function echo $i;
    so it outputs 4, then returns to the place where it was pushed onto the stack last time, and continues to output 3... until the fun function returns completely. Changing to
    if ($i < 5) { return fun($i);} will output 23455

    reply
    0
  • 某草草

    某草草2017-05-24 11:36:32

    You can refer to this article, and it will be clear if you understand recursion. http://www.jianshu.com/p/8bee...

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-24 11:36:32

    You can think of it this way, what this function does is that the first half echoes i, the second half echoes i, and a call to itself is inserted in the middle until i=5, which is equivalent to echoing i until 5, and then echoing back to the beginning. It’s worth it.. Just have a good relationship with the in and out.

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-24 11:36:32

    Thank you everyone, I understand

    reply
    0
  • Cancelreply