【问题标题】:Why the code is executing again even after the if condition returning false value?为什么即使在 if 条件返回 false 值之后代码仍会再次执行?
【发布时间】:2018-04-28 14:25:38
【问题描述】:

考虑下面的代码:

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

    $count++;
    echo $count."<br>";
    if($count < 2) {
      test();
      echo "In last line...".$count."<br>";
    }
    $count--;
    echo "Count Value : ".$count."<br>";
  }
  test();
?>

输出如下:

1
2
Count Value : 1
In last line...1
Count Value : 0

我对上面输出中用红色边框勾勒出来的以下部分感到困惑。

我想知道,如果在变为 $count = 2 时返回 false,紧随其后的代码行 echo "Count Value : ".$count."&lt;br&gt;"; 会被执行。然后,预计将停止流,因为它是最后一条语句。

  1. 为什么打印行数后程序流程没有停止 值:1?

  2. 那么输出的最后两行是如何生成的?

  3. 谁又在调用test()函数?

  4. 静态变量如何再次重置为 0 并打印输出的最后两行?

  5. 在递归中,函数调用执行后的剩余代码是否与测试条件失败时调用递归函数的次数一样多?

【问题讨论】:

  • 你知道什么是静态变量吗?再次输入函数时,它会保留该值。在这种情况下,它在第一次调用时为 0,然后您将其递增。检查 $count
  • 第一次,$count

标签: php function if-statement recursion


【解决方案1】:

一一解答。

1.为什么打印行 Count Value : 1 后程序流程没有停止?

=> 因为打印行 Count Value : 1 是在函数 test() 的第二次调用中完成的。

2.那么输出的最后两行是如何生成的?

=> 完成第二次通话后,仍需要完成第一次通话,再次开始

    echo "In last line...".$count."<br>";

3.谁又调用了test()函数?

=> 下面第二行代码 sn-p 再次调用函数。

    if($count < 2) {
      test();
      echo "In last line...".$count."<br>";
    }

3.静态变量如何再次重置为0并打印输出的最后两行?

=> 静态变量不会被重置,静态只会初始化变量一次,一旦启动它就不会打扰值。

4.递归函数调用后的剩余代码是否与测试条件失败时调用递归函数的次数一样多?

=> 是的

【讨论】:

    【解决方案2】:
    <?php
    function test() 
    {
        static $count = 0;
    
        $count++;
        echo $count."<br>";//first output is 1
        if($count < 2) //first time condition true(i.e 1<2) and second time it is false
       {
          test();
    /* here you again calling same function then $count will became 2 and you did not return anything here so next code will get executed after complete of 2nd time function. if you set **return false** here then your marked output does not execute*/
      echo "In last line...".$count."<br>";//Here $count will be 1 because its continuing first statement
        }
        $count--;//first time $count is 2 here but you are decremented here so 1
                 // second time $count is 1 because continuing first statement and again its decremented so value will became 0.
        echo "Count Value : ".$count."<br>";// first output is 1//second output is 0
    }
    test();
    ?>
    

    【讨论】:

    • 如果你在 if 条件下设置 return true/false 则剩余代码不会执行。
    【解决方案3】:

    请找到执行...

    1.first time calling test()
      -> initialized  static $count = 0;
      -> $count++//$count = 1
      -> echo $count."<br>";// output 1
      -> if($count < 2) { // here $count is 1 condition passed 
    
    2.calling from if statement test()
      -> static $count = 0; // as it is static variable will not lose its value when the function exits and will still hold that value should the function be called again // so here $count is 1
      -> $count++ //$count = 2
       -> echo $count."<br>";// output 2
      ->if($count < 2) { // here $count is 2 condition failed 
      -> $count--;$count is 1 
      -> output Count Value : 1
      -> calling from if statement test() is completed.
      -> prints next statement "In last line...".$count."<br>";//In last line...1 //value from first time calling test() // output In last line...1
      -> $count--;$count is 0
      -> Count Value : 0 
    

    【讨论】:

      猜你喜欢
      • 2018-06-25
      • 2023-01-01
      • 1970-01-01
      • 2022-09-29
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多