Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 递归函数块的最后一条语句执行了多少次?_Php_Function_Recursion_Static_Static Variables - Fatal编程技术网

Php 递归函数块的最后一条语句执行了多少次?

Php 递归函数块的最后一条语句执行了多少次?,php,function,recursion,static,static-variables,Php,Function,Recursion,Static,Static Variables,考虑下面演示递归的代码段: <?php function test() { static $count = 0; $count++; echo $count."<br>"; if($count < 10) { test(); } echo "Count Value : ".$count--; } test(); ?> 我期望函数test()的最后一个代码语句,即echo“Count Val

考虑下面演示递归的代码段:

<?php
  function test() {
    static $count = 0;

    $count++;
    echo $count."<br>";
    if($count < 10) {
      test();
    }
    echo "Count Value : ".$count--;
  }

  test();
?>
我期望函数
test()
的最后一个代码语句,即
echo“Count Value:”.$Count--$count=10时返回false时,code>将只执行一次一切都会结束

但出乎意料的是,我让它执行了十次,变量
$count
的值不断减小。我不明白这是怎么发生的?这里的代码流是如何被意外操纵的

由于递归函数调用是在if条件内进行的,因此即使在if条件失败后,如何在随后调用它10次以上

请给我解释一下


注意:我没有忘记添加其他内容,我不想要它。只需解释最后一条语句为什么以及如何仅在打印了从1到10的编号之后以及仅在if条件失败之后执行。当if条件返回true时,它不会被执行。怎么做?

我想你忘了其他的

<?php
  function test() {
    static $count = 0;

    $count++;
    echo $count."<br>";
    if($count < 10) {
      test(); // when this call is made, all the code bellow waits for it to return
    } else {
      echo "Count Value : ".$count--;
    }
  }

  test();
?>

您的代码从外部运行9次递归+1,因此总共执行10次应该是可以的。 这里有一个评论版本:

<?php
  function test() {
    static $count = 0; // initialize only the first run

    $count++;
    echo $count."<br>"; // Current $count status
    if($count < 10) {   // goes on until 9
      test();           // This function will run before everything else
    }
    // regardless the value of $count print $count then decreases it
    echo "Count Value : ".$count--; 
    //only the other calls in the stack will see by the -- operator effect

  }
  test();
?>


else
echo“计数值:”.$Count--函数的执行不会在重新调用时中断。代码的其余部分将在returning@splash58当前位置我没有忘记添加其他内容,我不想要它。只需解释最后一条语句为什么以及如何仅在打印了从1到10的编号之后以及仅在if条件失败之后执行。当if条件返回true时,它不会被执行。如何执行?函数的所有调用都在内部测试调用时暂停。你会看到越来越多的人。然后他们开始完成并执行从最深调用开始的其余代码,您可以看到降序似乎,@claudio answer的第二部分很好地说明了这个过程,假设max count是2。让我们在内部测试调用点添加函数的代码。这将是一件非常重要的事情。听着,我希望你能理解结果。我没有忘记添加其他内容,我也不想要。只需解释最后一条语句为什么以及如何仅在打印了从1到10的编号之后以及仅在if条件失败之后执行。当if条件返回true时,它不会被执行。怎么用?
<?php
  function test() {
    static $count = 0;

    $count++;
    echo $count."<br>";
    if($count < 10) {

      static $count = 1;

      $count++;
      echo $count."<br>";
      if($count < 10) {

        static $count = 2;

        $count++;
        echo $count."<br>";
        if($count < 10) {


          // ... the code repeats until the point when $count = 9

        } else {
          echo "Count Value : ".$count--;
        }

      } else {
        echo "Count Value : ".$count--;
      }


    } else {
      echo "Count Value : ".$count--;
    }
  }

  test();
?>
<?php
  function test() {
    static $count = 0; // initialize only the first run

    $count++;
    echo $count."<br>"; // Current $count status
    if($count < 10) {   // goes on until 9
      test();           // This function will run before everything else
    }
    // regardless the value of $count print $count then decreases it
    echo "Count Value : ".$count--; 
    //only the other calls in the stack will see by the -- operator effect

  }
  test();
?>