Re: BUG in recursion
| From: | Lars Torben Wilson | Date: | Sun, 31 Mar 2002 00:10:30 +0000 |
| Subject: | Re: BUG in recursion | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-90825@lists.php.net to get a copy of this message | ||
On Sat, 2002-03-30 at 15:59, Uros Gruber wrote:
> Hi!
>
> This is a code
>
> function Test()
> {
> static $count = 0;
>
> $count++;
> echo $count;
> if ($count < 10) {
> Test ();
> }
> echo "j";
> }
> test();
>
> Output is
>
> 12345678910jjjjjjjjjj
>
> Why is there 10 j at the and. If this would work it can be
> only one. If $count is grater than 10 it does not call itself
> but end. Is this maybe a bug or what.
No, there should be 10 of them, since every time Test() returns,
the function carries on executing. If you only want one 'j'
printed then put a return statement after the call to Test():
function Test()
{
static $count = 0;
$count++;
echo $count;
if ($count < 10) {
Test();
return;
}
echo "j";
}
Test();
Cheers!
Torben
--
Torben Wilson <torben@php.net>
http://www.thebuttlesschaps.com
http://www.hybrid17.com
http://www.inflatableeye.com
+1.604.709.0506