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.

Recommended Answers

All 2 Replies

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);

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.