note 125806 deleted from language.variables.scope by crell
| From: | crell@php.net | Date: | Thu, 31 Mar 2022 19:36:58 +0000 |
| Subject: | note 125806 deleted from language.variables.scope by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217925@lists.php.net to get a copy of this message | ||
Note Submitter: Duke dot Bouvier at NOPSAM dot Gmail dot com
----
The recursion example has some excess code:
"$count--" is superfluous.
"$count++" can be built into the loop (but be sure to use pre-increment not a
post-incremenet
Thus:
<?php
function test()
{
static $count = 1;
echo $count." ";
if (++$count < 10) {
test();
}
}
?>
Though with a static, all you are really doing is creating a loop but with lots of function calls
pushed onto the stack.
A great things about recursive functions is that you don't actually have to have any static
data. You simply supply an argument to pass the value (and can use a default value for the argument
for your initialisation if desired.)
<?php
function test($count = 1)
{
echo $count." ";
if ($count < 10) {
test(++$count);
}
}
test()
?>