2

Consider the below code snippet demonstrating recursion :

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

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

  test();
?>

Output of above code is as below :

1
2
3
4
5
6
7
8
9
10
Count Value : 10
Count Value : 9
Count Value : 8
Count Value : 7
Count Value : 6
Count Value : 5
Count Value : 4
Count Value : 3
Count Value : 2
Count Value : 1

I expected the last code statement of the function test() i.e. echo "Count Value : ".$count--; will get execute only once when the if condition returns false upon $count = 10; and everything would be finish.

But unexpectedly, I'm getting it executed ten times with decreasing value of variable $count. I'm not understanding how it's happening? How is the code flow getting manipulated here unexpectedly?

As the recursive function call is made inside the if condition how can it get called subsequently for 10 more times even after failing the if condition?

Please explain me.

Note : I haven't forgot to add else and I don't want it. Just explain why and how the last statement is getting executed only after printing nos. from 1 to 10 and only after the failing of if condition. When the if condition was returning true it was not getting executed. How?

PHPLover
  • 1
  • 51
  • 158
  • 311
  • 1
    **else** `echo "Count Value : ".$count--;` – splash58 Nov 15 '17 at 08:47
  • 1
    Execution of a function does not break at the point of a resursive call. The rest of the code will be executed after returning – splash58 Nov 15 '17 at 08:50
  • @splash58: I haven't forgot to add else and I don't want it. Just explain why and how the last statement is getting executed only after printing nos. from 1 to 10 and only after the failing of if condition. When the if condition was returning true it was not getting executed. How? – PHPLover Nov 15 '17 at 08:54
  • 1
    All calls of the function are paused at inner test call. And you see increasing count. Then they start to finish and execute rest of code beginning from the most deep call, and you see decreasing order – splash58 Nov 15 '17 at 08:58
  • @splash58 : I've still have few queries : 1. Do you mean that the code reaming after the inner test call which is present in if condition will not get executed unless the if condition returns false? 2. Will the outer function call be made again after if returning false? 3. What do you mean by the term most deep call? – PHPLover Nov 15 '17 at 09:35
  • 1
    Seems, that the 2nd part of @claudio answer get a good illustration of the process Just suppose max count is 2. Let's add code of the function at the point of inner test call. It will be something as https://eval.in/900429 . Look there, I hope, you will understand the result – splash58 Nov 15 '17 at 09:51
  • 1. before finishing recursive calls – splash58 Nov 15 '17 at 09:52

2 Answers2

3

I think you forgot the else.

<?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();
?>

What happens is that every time you call test(), inside the if condition, the execution stops until the newly called test() returns. The test() function only returns when $count >= 10. which means that all hanging functions call will continue. What is a RECURSIVE Function in PHP?

Your code can be translated to something like this;

<?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();
?>
Claudio
  • 5,078
  • 1
  • 22
  • 33
  • I haven't forgot to add else and I don't want it. Just explain why and how the last statement is getting executed only after printing nos. from 1 to 10 and only after the failing of if condition. When the if condition was returning true it was not getting executed. How? – PHPLover Nov 15 '17 at 08:51
1

Your code runs 9 times the recursion + 1 from outside, so it should be ok to have 10 total executions. Here a commented version:

<?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();
?>
sabau
  • 166
  • 1
  • 11