递归函数块的最后一条语句执行了多少次?
How many times the last statement of a recursive function block gets executed?
考虑下面演示递归的代码片段:
<?php
function test() {
static $count = 0;
$count++;
echo $count."<br>";
if($count < 10) {
test();
}
echo "Count Value : ".$count--;
}
test();
?>
以上代码的输出如下:
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
我预计函数 test()
的最后一个代码语句,即 echo "Count Value : ".$count--;
只会在 $count = 10;
上的 if 条件 returns 为假时执行一次,并且一切都会完成。
但出乎意料的是,我执行了 10 次,变量 $count
的值递减。我不明白这是怎么回事?代码流是如何意外地在这里被操纵的?
由于递归函数调用是在 if 条件内进行的,因此即使在 if 条件失败后,它又如何被后续调用 10 次以上?
请解释一下。
注:我还没忘记加else,我不要了。只需解释为什么以及如何仅在打印 nos 后才执行最后一条语句。从 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();
?>
每次调用 test() 时,在 if 条件内,执行都会停止,直到新调用的 test() returns。当 $count >= 10
时,test() 函数仅 returns。这意味着所有挂起的函数调用将继续。 What is a RECURSIVE Function in PHP?
您的代码可以翻译成这样;
<?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();
?>
您的代码从外部运行了 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();
?>
考虑下面演示递归的代码片段:
<?php
function test() {
static $count = 0;
$count++;
echo $count."<br>";
if($count < 10) {
test();
}
echo "Count Value : ".$count--;
}
test();
?>
以上代码的输出如下:
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
我预计函数 test()
的最后一个代码语句,即 echo "Count Value : ".$count--;
只会在 $count = 10;
上的 if 条件 returns 为假时执行一次,并且一切都会完成。
但出乎意料的是,我执行了 10 次,变量 $count
的值递减。我不明白这是怎么回事?代码流是如何意外地在这里被操纵的?
由于递归函数调用是在 if 条件内进行的,因此即使在 if 条件失败后,它又如何被后续调用 10 次以上?
请解释一下。
注:我还没忘记加else,我不要了。只需解释为什么以及如何仅在打印 nos 后才执行最后一条语句。从 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();
?>
每次调用 test() 时,在 if 条件内,执行都会停止,直到新调用的 test() returns。当 $count >= 10
时,test() 函数仅 returns。这意味着所有挂起的函数调用将继续。 What is a RECURSIVE Function in PHP?
您的代码可以翻译成这样;
<?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();
?>
您的代码从外部运行了 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();
?>