Re[4]: [PHP] BUG in recursion
| From: | Uros Gruber | Date: | Sun, 31 Mar 2002 00:46:28 +0000 |
| Subject: | Re[4]: [PHP] BUG in recursion | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-90835@lists.php.net to get a copy of this message | ||
Hi,
Ok, no I understand why. So this explains why every function
must have return. But how can i program for example one
recursion or where to put return so that test would not be
waiting for return but at the end of this recursion i would
like to do something with that $count for example.
Hmm i wrote this very strange i hope you understand.
I tell it simpler.
I would like something is executed x many times (for such
long if sentence is true) and thene do sometnihg else and
continue with program, not looping here.
--
tia,
Uros mailto:uros.gruber@sir-mag.com
Sunday, March 31, 2002, 1:34:20 AM, you wrote:
LTW> On Sat, 2002-03-30 at 16:16, Uros Gruber wrote:
>> Hi!
>>
>> This I simply don't understand why this code executes that
>> way. So there is only inportant to put return after recursive
>> call and it works like i wan't. I sometimes write small C
>> programs and I don't remeber that code like this have to work
>> that way.... Hm.
LTW> Well, that's how it's supposed to work. Look at it like this:
LTW> 1: function test() {
LTW> 2: static $count = 0;
LTW> 3: $count++;
LTW> 4: echo $count;
LTW> 5: if ($count < 10) {
LTW> 6: test();
LTW> 7: }
LTW> 8: echo 'j';
LTW> 9: }
LTW> So the first thing that happens is you call test(). It executes
LTW> its lines in order, including function calls. When it hits the
LTW> call to test() on line 6, it calls test(), waits for it to
LTW> return, and then continues on to line 8, echoing 'j'.
LTW> This happens every time through, so when you get up to $i == 10, you
LTW> have 10 test()s stacked up, each of them waiting on line 6 for the
LTW> function call to test() to return. As each test() call returns,
LTW> execution continues on to line 8, echos j, then returns.
LTW> I'm in a hurry so I don't have too much time to make this much
LTW> clearer (and my brain is not so good today anyway). If this still
LTW> doesn't make sense then Google gives a zillion hits for 'introduction
LTW> to recursion'.
LTW> Hope this helps,
LTW> Torben
>> --
>> bye,
>> Uros mailto:uros.gruber@sir-mag.com
>>
>>
>> Sunday, March 31, 2002, 1:10:30 AM, you wrote:
>>
>> LTW> 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.
>>
>> LTW> No, there should be 10 of them, since every time Test() returns,
>> LTW> the function carries on executing. If you only want one 'j'
>> LTW> printed then put a return statement after the call to Test():
>>
>> LTW> function Test()
>> LTW> {
>> LTW> static $count = 0;
>>
>> LTW> $count++;
>> LTW> echo $count;
>> LTW> if ($count < 10) {
>> LTW> Test();
>> LTW> return;
>> LTW> }
>> LTW> echo "j";
>> LTW> }
>> LTW> Test();
>>
>>
>> LTW> Cheers!
>>
>> LTW> Torben
>>
>>
>> LTW> --
>> LTW> Torben Wilson <torben@php.net>
>> LTW> http://www.thebuttlesschaps.com
>> LTW> http://www.hybrid17.com
>> LTW> http://www.inflatableeye.com
>> LTW> +1.604.709.0506
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>