473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Recursionalism

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;

May 4 '07 #1
9 1156

<bl*************@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
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;
}
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){recursion($newvalue);}
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

May 4 '07 #2
bl*************@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

May 4 '07 #3
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

May 4 '07 #4

"Mohawk Moak" <bl*************@gmail.comwrote in message
news:11**********************@c35g2000hsg.googlegr oups.com...
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
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
May 4 '07 #5
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
May 4 '07 #6
Mohawk Moak wrote:
this method worked good, i did not realize to set the global twice.
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
May 4 '07 #7
"Toby A Inkster" <us**********@tobyinkster.co.ukwrote in message
news:na************@ophelia.g5n.co.uk...
Mohawk Moak wrote:
>this method worked good, i did not realize to set the global twice.

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.

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...

--
Ra*********@gmail.com

"Good tea. Nice house." -- Worf
May 4 '07 #8

"Toby A Inkster" <us**********@tobyinkster.co.ukwrote in message
news:na************@ophelia.g5n.co.uk...
Mohawk Moak wrote:
>this method worked good, i did not realize to set the global twice.

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.
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.
May 4 '07 #9
i find goshas solution most elegant, and i am all about trying to keep
my code clean, so thanks to all again

May 4 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.