0

Ok - I've got an easy one for you all on this Friday. I know the answer is staring me in the face, but I can't seem to figure it out. The problem is that the following function does not return a value for me. However, if I insert a print statement before the 'return $new_id' line, the correct id is printed to the screen.

Also, if I provide an oID that returns 0 on the first try, the function returns a value. Otherwise, it loops as expected, exits at the return statement, but no ID is returned. No infinite loop is created and no other errors can be found.

function GetRefundOrderNumber($oID,$try=0)
{
    $new_id = $oID ."-R";
    
    if ($try != 0)
    {
        $new_id .= $try;
    }
    
    $query = "SELECT orders_id FROM orders WHERE refund_orders_id='$new_id'";
    $query_res = mysql_query($query);
    
    if (mysql_num_rows($query_res) === 0)
    {
        return $new_id;
    }
    else
    {
        $try++;
        GetRefundOrderNumber($oID,$try);
    }
}

$id = GetRefundOrderNumber(163308);
print "ID = $id";

Any suggestions would be appreciated.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by conord
1

You don't have any return statement after the recursive call. If you need a value back after the recursion terminates, you will need to store the recursion's return value then provide a subsequent return to get it out of the function.

You could probably even use return GetRefundOrderNumber($oID,$try);

Edited by Fbody: n/a

0

You don't have any return statement after the recursive call. If you need a value back after the recursion terminates, you will need to store the recursion's return value then provide a subsequent return to get it out of the function.

You could probably even use return GetRefundOrderNumber($oID,$try);

Interesting. Knew it was something simple. Thanks!

Edited by conord: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.