|
Posted by gosha bine on 05/04/07 07:43
On 04.05.2007 05:36 Mohawk Moak wrote:
> this method worked good, i did not realize to set the global twice.
> the reason to set the counter to 0 in the end is so that the function
> is usable more than 10 times (or whatever limit it is)
> thanks for pointing out that it was in the wrong row though xD
> probably would not have figuered that one out so fast
> bless
>
Actually, you only need 'global' once: inside the function. Moreover,
even this is not really necessary - using global variables is considered
bad practice and it's better to avoid them from the beginning. This is
how you can rewrite your function without globals:
function recursion($value, $counter = 0){
$counter++;
if ($counter > 10){exit("Recursion exceeded limit!");}
$newvalue= blabla something with $value
if (condition applies){recursion($newvalue, $counter);}
return $newvalue;
}
--
gosha bine
extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Navigation:
[Reply to this message]
|