| 
	
 | 
 Posted by Norman Peelman on 05/04/07 02:18 
blessblessbless@gmail.com wrote: 
> Hey all, 
> I am self taught in PHP and my method is just trial and error most of 
> the time, but when trying doe not help anymore I get frustrated... I'm 
> sure some of you know what I mean. 
>  
> My problem is with a recursive(referring back to itself) function I am 
> trying to write. 
> The function works well on its own, and it also works recursively as 
> many times as I want it to. 
> I am going to give you a lite version of the function because it 
> doesn't really matter what it does. 
>  
> global $counter; 
> $counter=0; 
> function recursion($value){ 
> $counter++; 
> if ($counter > 10){exit("Recursion exceeded limit!");} 
>  
> $newvalue= blabla something with $value 
>  
> if (condition applies){recursion($newvalue);} 
> return $newvalue; 
> $counter=0; 
> } 
>  
> SO my problem is, the counter is not working. I know it has something 
> to do with the fact that functions use their own variables, buts that 
> is why I thought to set the counter as global :/ 
> Anybody have an idea why it wont stop and my apache crashes when I try 
> to repeat the function limitless? (ok i know why it crashes, just not 
> why the function does not stop; 
>  
 
function recursion($value){ 
static $counter; 
$counter++; 
if ($counter > 10){exit("Recursion exceeded limit!");} 
 
$newvalue= blabla something with $value 
 
if (condition applies){recursion($newvalue);} 
return $newvalue; 
$counter=0; 
} 
 
$the_value = recursion(5); 
 
The 'static' keyword causes the $counter variable to retain it's value. 
 
Norm
 
  
Navigation:
[Reply to this message] 
 |