Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder o0O0o.o0O0o's Avatar
    Join Date
    Jan 2008
    Location
    C:\Windows\System32
    Posts
    1,018
    Thanks
    19
    Thanked 9 Times in 9 Posts

    recursion function problem

    hi


    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


    Code:
    function (parentid , depth)
    {
    
    $display . = ...
    
    function(parentid,depth)
    
    ......
    
    }

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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:
    PHP Code:
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •