mercredi 15 novembre 2017

Why the code is executing again after the if condition returning false value?

Consider below code :

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

Output is as below :

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

I'm confused about the following part from the above output

which is outlined with red colored border.

I want to know after if returning false on becoming $count = 2 the immediately following code line echo "Count Value : ".$count."<br>"; gets executed. Then, it is expected to stop the flow as it's the last statement.

  1. Why the program flow is not stopping after printing the line Count Value : 1?

  2. Then how the last two lines from the output are generating?

  3. Who is calling the test() function again?

  4. How does the static variable getting reset to 0 again and printing the last two lines of the output?

Aucun commentaire:

Enregistrer un commentaire