function test(){ static $count = 0; $count++; echo "-- ".$count." --\n"; if ($count < 10) { test(); } $count--; echo "## ".$count." ##\n";}test();
The resulting output is as follows:
-- 1 ---- 2 ---- 3 ---- 4 ---- 5 ---- 6 ---- 7 ---- 8 ---- 9 ---- 10 --## 9 #### 8 #### 7 #### 6 #### 5 #### 4 #### 3 #### 2 #### 1 #### 0 ##
My doubts are that when $count
added to 10 will not call itself again, then it will run the following code $count--
and then the output is finished, but why it is still running 9 times, ask the expert answer.
Reply content:
function test(){ static $count = 0; $count++; echo "-- ".$count." --\n"; if ($count < 10) { test(); } $count--; echo "## ".$count." ##\n";}test();
The resulting output is as follows:
-- 1 ---- 2 ---- 3 ---- 4 ---- 5 ---- 6 ---- 7 ---- 8 ---- 9 ---- 10 --## 9 #### 8 #### 7 #### 6 #### 5 #### 4 #### 3 #### 2 #### 1 #### 0 ##
My doubts are that when $count
added to 10 will not call itself again, then it will run the following code $count--
and then the output is finished, but why it is still running 9 times, ask the expert answer.
Well, let's assume that $count is less than 2 o'clock to see the entire execution process:
Change less than 10 to less than 2:
function test() { static $count = 0; $count++; echo "-- ".$count." --\n"; if ($count < 2) { test(); } $count--; echo "## ".$count." ##\n"; }
2. Replace the recursive test () with the code in the body of the function that is less than 2 o'clock:
function test(){ static $count = 0; // line 1 $count++; // line 2 echo "-- ".$count." --\n"; // line 3 if ($count < 2) { // line4 static $count = 0; $count++; // line 5 echo "-- ".$count." --\n";// line 6 if ($count < 2) {// line 7 test(); } $count--;// line 8 echo "## ".$count." ##\n";// line 9 } $count--;// line 10 echo "## ".$count." ##\n";// line 11 }
3. After calling Test ()
test()
4. Take a look at the entire detailed implementation process:
4.1 第一次,line 1:$count = 0; 4.2 执行line 2后,$count = 1; 4.3 所以在line 3 会输出: **-- 1 --** 4.4 接着执行line 4,由于现在 $count < 2为真; 4.5 所以执行 line5后变为2 4.6 所以 line 6 会输出: **-- 2 --** 4.7 然后到 line 7,由于 $count = 2,不小于2直接执行line 8 4.8 line 8 执行后, $count变为1,接着执行line 9 4.9 在line 9 会输出: **## 1 ##** 4.10 接着就执行 line 10,$count再减1,就变为0 4.11 因此在 line 11会输出: **## 0 ##**
5. Summary:
When we assume that $count is less than 2 o'clock, we see in the detailed execution process above:
Test () was executed 2 times in total ,
The output is:
--1--,--2--, # 1 ##,## 0 # #
At this time again back will $count less than 10, it is easy to explain the landlord's doubts.
I don't know if it helps you. Keep in touch with questions.
No more running 9 times, the test
function only runs 10 times. However, there are two outputs in each function, so the total output is 20 times.
There is no return will not stop, after calling himself will continue to execute the
Because when the $count
=10, the code is still executed, you can write
function test(){ static $count = 0; $count++; echo "-- ".$count." --
"; if ($count < 10) { test(); $count--; echo "## ".$count." ##
"; }}test();
@phping This is my understanding of just recursion, similar to yours.
function test() { static $count = 0; // 初始化静态变量$count $count++; // $count = 1; echo "-- ".$count." --\n"; // 输出 1 if($count < 3) { // 1 小于 3 为真 static $count = 0; // 初始 $count++; // $count = 2; echo "-- ".$count." --\n"; // 输出 2 if($count < 3) { // 2 小于 3 为真 static $count = 0; // 初始 $count++; // $count = 3 echo "-- ".$count." --\n"; // 输出 3 if($count < 3) {} // 3 不小于 3 为假 $count--; // $count = 2; echo "## ".$count." ##\n"; // 减后输出 2 } $count--; // $count = 1 echo "## ".$count." ##\n"; // 减后输出 1 } $count--; // $count = 0; echo "## ".$count." ##\n"; // 减后输出 0}test();
Final output
-- 1 ---- 2 ---- 3 --## 2 #### 1 #### 0 ##