Recursive Function returning emptry string

I have a recursive function that mysteriously returns an empty string.

I’m completely flummoxed by this behavior. In trying to debug, I have the function echo the function name and its arguments. I also have the function return a value upon termination and I echo this as well. One might think that there is an error in the function, but I even echo the string I’m going to return right before the return statement and I get the desired value, but somehow this value disappears when the function returns.

Since this involves some SQL queries, I’m omitting the actual code but here is the gist of the function and its output in psuedocode.

Code:

<?php
function Recur($name, $id)
{        //actual code
        echo 'Recur('.$name.','.$id.')<br>';
        if($id == 0)
                return $name;
        //end comment
        //pseudo code
        else{
        //do something
                Recur($tmpname, $id-1);
        }
        //end comment
}

        $result = Recur($name, $id);
        echo $result;
?>


Here is actual output:

Quote:

Recur(Updated Account, 0)
Updated Account
Recur(TestAccount1, 1)
Recur(Updated Account:TestAccount1, 0)

Recur(Test Account3, 0)
Test Account3


When I remove the echo $result line and insert echo $name before return $name, I get the same output as above, but the blank line is replaced with Updated Account:TestAccount1 as expected.

Has anyone seen this behavior before?

I might just give up and write a while loop. Oh PHP you disappoint me!

View Complete Thread with Replies

No Reply.
Find related on my parent xResources.

Looking for something else? Search for it...

Tagged with: , , , ,

No Replies

There are still no reply for this post.

Leave your reply...

If you want to leave your reply for this post, simply fill out the next form:




You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .


Powered by: Wordpress & 3G Creations