View Full Version : recursion function problem

01-22-2008, 11:51 PM

i am using recursion function to display the tree based menu.
Initially i was echoing it line by line and it worked fine
but now i want to append the output to variable

function (parentid , depth)

$display . = ...




Now i want is that when the function finishes it returns the ouput

But how can i made the check that function has reached the last menu and now it should return the $display

not anywhere in between

01-23-2008, 01:03 AM
Not sure what you are looking at doing, since you don't have any conditional control in your example. At the 'end' of a function you return the result, once it reaches the end the recursive 'stack', it will step up each time and return the result. Generally if you want to return a result from a recursive function you would append a result from a function call within the function, or run against a static variable.
For example:

function recurseSomething($something)
$result = '';
if (is_array($something))
foreach($something AS $nothing)
$result .= recurseSomething($nothing);
else if (is_string($something))
$result .= strtoupper($something);

return $result;


Recursion is all about the conditions you have placed upon it. Without knowing that, I can't really recommend exactly what you should do to preform a result.
Remember, you can always run against a static variable which is a reference to a calling scoped variable - that will actually eliminate your need to perform a return.