# Recursionalism

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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)
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;

## Re: Recursionalism

Your counter is not global. you are creating a new counter variable each
recursive step that is local to the function. So the the first if statement
never does anything.
use global \$counter inside the function

global \$counter;
\$counter=0;

function recursion(\$value){
global \$counter;
\$counter++;
if (\$counter > 10){exit("Recursion exceeded limit!");}

\$newvalue= blabla something with \$value

if (condition applies)
return \$newvalue;
\$counter=0;
}

Your last line \$counter=0 is also not doing anything. your returning so
anything after that point isn't going to be called. Its also probably not a
good idea to do that unless you know exactly what your doing. (in this case
its ok because it doesn't ever get called but in some circumstances it could
end up causing an infinite loop).

Jon

## Re: Recursionalism

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

## Re: Recursionalism

Yeah. See Normans post about static variables. Normally this is what you use
but I didn't know if php had it(though that is what global was).

If you get a program like zend studio then you can debug your code and step
through it one line at a time and see whats going on. This is how I found
out that your counter was local and not actually counting the recursive
steps.  It makes it much easier to write code if you can debug it.

Jon

## Re: Recursionalism

On 04.05.2007 05:36 Mohawk Moak wrote:

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

## Re: Recursionalism

i find goshas solution most elegant, and i am all about trying to keep
my code clean, so thanks to all again

## Re: Recursionalism

Mohawk Moak wrote:

You don't -- you only need to use it inside the function. Jon's use of
"global" outside the function is entirely redundant, though doesn't cause
any harm.

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux

## Re: Recursionalism

If you want to get technical, it's actually not even redundant, since it
does _nothing_. If it repeated something that already was done, then it
would be redundant.

And as for the harm, since it does nothing but litter the source code,
better take it out. That way it doesn't confuse the next person reading the
source...

--
Rami.Elomaa@gmail.com

"Good tea. Nice house." -- Worf

## Re: Recursionalism

Oh, lol... I didn't read is post very well. Yes it is redudant. Didn't mean
it to be there.  In any case its probably best to use static. In this case
it might work but even redudancy can cause problems.

## Re: Recursionalism

blessblessbless@gmail.com wrote:

function recursion(\$value){
static \$counter;
\$counter++;
if (\$counter > 10){exit("Recursion exceeded limit!");}

\$newvalue= blabla something with \$value

if (condition applies)
return \$newvalue;
\$counter=0;
}

\$the_value = recursion(5);

The 'static' keyword causes the \$counter variable to retain it's value.

Norm