php递归注意事项

/* 循环去除字符串左边的0 */
function removeLeftZero($str){
    if($str['0'] == '0'){
        $str = substr($str, '1');
        removeLeftZero($str);
    }else{
        return $str;
    }
}

这样运行以后如果是递归是不会有返回值的,递归后即使满足else条件也不会有返回值,应该改为

/* 循环去除字符串左边的0 */
function removeLeftZero($str){
    if($str['0'] == '0'){
        $str = substr($str, '1');
        return removeLeftZero($str);
    }else{
        return $str;
    }
}

也就是说在要递归的函数内部的函数前面要加return 

原文地址:https://www.cnblogs.com/firstcsharp/p/10915847.html