note 97387 deleted from language.variables.scope by danbrown
| From: | danbrown@php.net | Date: | Sat, 17 Apr 2010 00:42:19 +0000 |
| Subject: | note 97387 deleted from language.variables.scope by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168339@lists.php.net to get a copy of this message | ||
Note Submitter: blackjeffrey1 at gmail dot com
----
When functions call themselves, static variables aren't always the best option, while it will
work for
function test()
{
static $count = 0;
$count++;
echo $count;
if ($count < 10) {
test();
}
$count--;
}
It wont always work for ones which call themselves in a loop (I don't have a simple example to
show), but instead of counting recursions, it will simply count how many times the function is
called, I think a much better way is as such:
function test($count=0)
{
$count++;
echo $count;
if ($count < 10) {
test($count);
}
}
This still does the function of counting to 10 from 1 each time the function is called from outside
itself.