PHP recursive function return null return issue

 

Some time ago encountered when writing a recursive function of a problem, it has been the return value is null, the record here.

Write a small example:

/**
 * @param $i
 * @return mixed
 */
function recursion($i)
{
    if($i > 10){
        return $i;
    }else{
        $i++;
        recursion($i);
    }
}
echo recursion(1);

 

Examples of the above-described return null, mainly due to the function is called when there is no return, plus a return in recursion ($ i) in front of it.

/**
 * @param $i
 * @return mixed
 */
function recursion($i)
{
    if($i > 10){
        return $i;
    }else{
        $i++;
        return recursion($i);
    }
}
echo recursion(1);

Successful operation.

 

Guess you like

Origin www.cnblogs.com/woods1815/p/11108898.html