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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

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

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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);
}
(#10850)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

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

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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--;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

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

Post 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
Post Reply