PHP 手动示例 #6 具有递归函数变化范围理解的静态变量
PHP Manual Example #6 Static variables with recursive functions variation scope understanding
提前致歉(这里是新手),但试图理解并很难找到与此直接相关的主题。我使用 php 手册
中的递归函数对示例 #6 进行了变体
http://php.net/manual/en/language.variables.scope.php
<?php
function test() {
static $count = 0;
$count++;
echo $count;
if ($count < 10) {
test();
}
$count--;
echo $count;
}
test();
?>
输出为 123456789109876543210
我希望它在 if 语句之外达到 9 并递减后停止。
(例)123456789109
我显然不理解静态作用域和代码流。我真的应该想出一个调试器,但是有任何指示吗?
也许这个小小的修改可以帮助你更好地理解。
为什么在if语句外走到9还不停?
因为每次测试调用都会运行测试直到结束。它增加了 10 次,减少了 10 次。由于 10 次测试调用,首先将它增加 10 次,并在第 10 次调用 decementing beginns。第 10 次调用结束,第 9 次调用递减。第 9 次调用结束,第 8 次调用递减....
<?php
function test()
{
static $count = 0;
$count++;
echo $count.". call of test() (output after incrementing)<br />";
if ($count < 10) {
test();
}
echo $count.". call of test() (output before decrementing)<br />";
$count--;
}
test();
?>
输出:
1. call of test() (output after incrementing)
2. call of test() (output after incrementing)
3. call of test() (output after incrementing)
4. call of test() (output after incrementing)
5. call of test() (output after incrementing)
6. call of test() (output after incrementing)
7. call of test() (output after incrementing)
8. call of test() (output after incrementing)
9. call of test() (output after incrementing)
10. call of test() (output after incrementing)
10. call of test() (output before decrementing)
9. call of test() (output before decrementing)
8. call of test() (output before decrementing)
7. call of test() (output before decrementing)
6. call of test() (output before decrementing)
5. call of test() (output before decrementing)
4. call of test() (output before decrementing)
3. call of test() (output before decrementing)
2. call of test() (output before decrementing)
1. call of test() (output before decrementing)
提前致歉(这里是新手),但试图理解并很难找到与此直接相关的主题。我使用 php 手册
中的递归函数对示例 #6 进行了变体http://php.net/manual/en/language.variables.scope.php
<?php
function test() {
static $count = 0;
$count++;
echo $count;
if ($count < 10) {
test();
}
$count--;
echo $count;
}
test();
?>
输出为 123456789109876543210
我希望它在 if 语句之外达到 9 并递减后停止。 (例)123456789109
我显然不理解静态作用域和代码流。我真的应该想出一个调试器,但是有任何指示吗?
也许这个小小的修改可以帮助你更好地理解。
为什么在if语句外走到9还不停?
因为每次测试调用都会运行测试直到结束。它增加了 10 次,减少了 10 次。由于 10 次测试调用,首先将它增加 10 次,并在第 10 次调用 decementing beginns。第 10 次调用结束,第 9 次调用递减。第 9 次调用结束,第 8 次调用递减....
<?php
function test()
{
static $count = 0;
$count++;
echo $count.". call of test() (output after incrementing)<br />";
if ($count < 10) {
test();
}
echo $count.". call of test() (output before decrementing)<br />";
$count--;
}
test();
?>
输出:
1. call of test() (output after incrementing)
2. call of test() (output after incrementing)
3. call of test() (output after incrementing)
4. call of test() (output after incrementing)
5. call of test() (output after incrementing)
6. call of test() (output after incrementing)
7. call of test() (output after incrementing)
8. call of test() (output after incrementing)
9. call of test() (output after incrementing)
10. call of test() (output after incrementing)
10. call of test() (output before decrementing)
9. call of test() (output before decrementing)
8. call of test() (output before decrementing)
7. call of test() (output before decrementing)
6. call of test() (output before decrementing)
5. call of test() (output before decrementing)
4. call of test() (output before decrementing)
3. call of test() (output before decrementing)
2. call of test() (output before decrementing)
1. call of test() (output before decrementing)