php递归函数return问题

    xiaoxiao2021-12-14  21

    在递归函数中返回值问题

    /* 循环去除字符串左边的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; } }

    转载请注明原文地址: https://ju.6miu.com/read-968386.html

    最新回复(0)