任何人都可以解释为什么我使用以下脚本获取此输出
输出
1:第一 2第一 3first 4first 5第一 6第一 7first 8first 9首页 10first 10 9 8 7 6 五 4 3 2 1
function test() {
static $count = 0;
$count++;
echo $count."first";
echo "<br>";
if ($count < 10) {
test();
}
echo $count--;
echo "<br>";
return $count."a"; }
答案 0 :(得分:0)
十次案件太复杂而无法解释。在三次案例中,它更容易
1st test() => $count++; show '1first'; call next test();
2nd test() => $count++; show '2first'; call next test()
3rd test() => $count++; show '3first' $count--; show '3'; return
4th $count--; show '2'; return
5th $count--; show '1'; return
编辑:
call call
test() 1 -> test() 2 -> test() 3
1 <- 2 <- 3
return return
或
test()
echo ++$count
call ---------------> test()
echo ++$count
call ---------------> test()
echo ++$count
echo $count--
echo $count-- <------ return
echo $count-- <------ return
return
答案 1 :(得分:0)
另外解释函数如何在堆栈环境中运行
先进先出
我只会在运行模式下对计数值进行评论&lt; 3 强>
// - First Call -
static $count = 0;
$count++; // count 1
echo $count."first"; // echo count 1first
echo "<br>";
if ($count < 3) {
test();
}
// - Second Call -
static $count = 0; // ignore
$count++; // count 2
echo $count."first"; // echo count 2first
echo "<br>";
if ($count < 3) {
test();
}
// - Third Call -
static $count = 0; // ignore
$count++; // count 3
echo $count."first"; // echo count 3first
echo "<br>";
if ($count < 3) {
test(); // no more calling so we can continue to stack
}
// - Third Call Continues-
echo $count--; // echo count 3, count is now 2
echo "<br>";
return $count."a";
// - Second Call Continues-
echo $count--; // echo count 2, count is now 1
echo "<br>";
return $count."a";
// - First Call Continues-
echo $count--; // echo count 1, count is now 0
echo "<br>";
return $count."a";