Recursive function not returning value

I can’t seem to figure out what’s going on with the following recursive function. If I echo out the variable within the function to test it out, it shows the value correctly. But if I try to return the value, it does not return the value.

Here it is:

function get_category_type($id)
{
    $id=intval($id);
    $s="select * from jsm_products_cats where listing_cat_id = ".$id; 
    $q=mysql_query($s) or die(mysql_error().'  sql:'.$s); 
    $d=mysql_fetch_array($q);
    if ( $d['listing_parent_cat_id'] == 0 ):
        $data = $d['listing_cat_id'];
        echo '<h1>It shows it here: '.$data.'</h1>';
        return $data;
    else:
        get_category_type($d['listing_parent_cat_id']);        
    endif;
}
$listing_type_id=get_category_type(6544); 
echo'<h1>Id does not show it here: '.$listing_type_id.'</h1>';

This is very dangerous code as well as being obsolete and completely removed from Php. If you have this on the net take it down right away. If you haven’t been hacked already, you will be.

You need to use PDO with prepared statements. https://phpdelusions.net/pdo

This function is only ran on an internal application that doesn’t request any user input, so I’m not really worried about it being “very dangerous code”. I just want to understand why the function shows the data inside the function ,but does not return the same data outside the function. I’m fully aware about functions getting deprecated and I appreciate your reply, but I’m just trying to figure out and understand exactly WHY it’s not returning the data.

The problem is that when you call it recursively from inside the function, you don’t do anything with the return value:

    else:
        get_category_type($d['listing_parent_cat_id']);        

When you call the function recursively here, your second iteration will return a value to this calling line when it finds the zero value. At that point, you need to return that value to the first calling function rather than just dropping it. Reading elsewhere (I haven’t done much recursion) it looks as if you should do:

    else:
        return get_category_type($d['listing_parent_cat_id']);        

Each time your function calls itself, it needs to be able to pass the return value up the call stack. Right now you only pass it back for the time when it actually finds the zero value, but if that was anything other than the first call, it will be lost when the second or subsequent function ends. So your return() in the original code doesn’t pass control back to the calling main code, it passes it back to the previous function, and so on, until all the calls are returned from.

Another way to look at it is to forget about the recursive aspect of things and consider two functions - getcat() and gettopcat(). If getcat() doesn’t find the answer, it calls gettopcat(). It follows that when gettopcat() returns the value to getcat(), then getcat() must also return it.

1 Like

Many thanks droopsnoot as your reply is exactly what I was looking for! I knew it was something simple I was overlooking.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.