Page 1 of 1

How to check if the function call is first or not in recursi

Posted: Thu Dec 31, 2009 2:03 am
by PHPycho
Suppose i have following recursive function(dummy):

Code: Select all

function someRecursiceFunc($id){    
    if(/*check_if_call_is_first*/){
        //want to do some operations here with the $id
    }
    $sql        = "SELECT id, parent_id, title FROM some_table WHERE id = ".(int)$id;
    $db_result  = mysql_query($sql);
    while($row  = mysql_fetch_array($db_result)){
        someRecursiveFunc($row['parent_id']);
    }
}
i tried with the static $count = 0 & incremented in while() loop
but this caused problem in function call inside loop(as static variable preserves the value)
For example:

Code: Select all

foreach($datas as $data){
    someRecursiveFunc($data['id']);//value of $count went increasing
}
is there any techinque so that i can check for first call?

Re: How to check if the function call is first or not in recursi

Posted: Thu Dec 31, 2009 2:15 am
by Christopher

Code: Select all

function someRecursiceFunc($id, $call_is_first){    
    if($call_is_first){
        //want to do some operations here with the $id
    }
    $sql        = "SELECT id, parent_id, title FROM some_table WHERE id = ".(int)$id;
    $db_result  = mysql_query($sql);
    while($row  = mysql_fetch_array($db_result)){
        someRecursiveFunc($row['parent_id'], false);
    }
}
foreach($datas as $data){
    someRecursiveFunc($data['id'], true);
}

Re: How to check if the function call is first or not in recursi

Posted: Thu Dec 31, 2009 2:23 am
by requinix
The $count method works if you increment before the recursive call and decrement after. Then $count tells how many recursive calls are ahead of the current one in the call stack.

I'm on the fence as to whether that's better than adding a function parameter (as arborint did). The former has a nice kind of simplicity, but feels like a "hack" when compared to the latter.

Re: How to check if the function call is first or not in recursi

Posted: Thu Dec 31, 2009 5:09 am
by PHPycho
tasairis wrote:The $count method works if you increment before the recursive call and decrement after. Then $count tells how many recursive calls are ahead of the current one in the call stack.

I'm on the fence as to whether that's better than adding a function parameter (as arborint did). The former has a nice kind of simplicity, but feels like a "hack" when compared to the latter.
Can you provide some example?

Re: How to check if the function call is first or not in recursi

Posted: Thu Dec 31, 2009 10:37 am
by AbraCadaver
tasairis wrote:The $count method works if you increment before the recursive call and decrement after. Then $count tells how many recursive calls are ahead of the current one in the call stack.

I'm on the fence as to whether that's better than adding a function parameter (as arborint did). The former has a nice kind of simplicity, but feels like a "hack" when compared to the latter.
Actually to keep an extra parameter out, I would set $call_is_first to default to true:

Code: Select all

function someRecursiveFunc($id, $call_is_first=true){
    //snipped   
}
But to use the count, this might work:

Code: Select all

function someRecursiceFunc($id){   
    static $count = 0;
    $count++;
 
    if($count == 1){
        //want to do some operations here with the $id
    }
    $sql        = "SELECT id, parent_id, title FROM some_table WHERE id = ".(int)$id;
    $db_result  = mysql_query($sql);
    while($row  = mysql_fetch_array($db_result)){
        someRecursiveFunc($row['parent_id']);
    }
    $count--;
}

Re: How to check if the function call is first or not in recursi

Posted: Thu Dec 31, 2009 11:21 am
by SimpleManWeb
Here is how I would probably handle it.

Code: Select all

 
    function someRecursiceFunc($id, $First){   
        if($First){
            //want to do some operations here with the $id
        }
        $sql        = "SELECT id, parent_id, title FROM some_table WHERE id = ".(int)$id;
        $db_result  = mysql_query($sql);
        while($row  = mysql_fetch_array($db_result)){
            someRecursiveFunc($row['parent_id']);
        }
   }
 

Code: Select all

 
   $First = true;
   foreach($datas as $data){
        someRecursiveFunc($data['id'], $First);
        $First = false;
   }
 

That should do the trick