我们在使用这是开始写的:
这段代码看起来没有问题,其实有else里面是有问题的。在这里执行的test没有返回值。所以虽然满足条件$i
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li><span>function test($i) </span></li> <li class="alt"><span>{ </span></li> <li> <span>$i </span><span class="attribute">-</span><span>= </span><span class="attribute-value">4</span><span>; </span> </li> <li class="alt"> <span>if($i </span><span class="tag"><span> </span><span class="tag-name">3</span><span>) </span></span> </li> <li><span>{ </span></li> <li class="alt"><span>return $i; </span></li> <li><span>} </span></li> <li class="alt"><span>else </span></li> <li><span>{ </span></li> <li class="alt"><span>return test($i); //增加return, 让函数返回值 </span></li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li><span>echo test(30); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
以上代码示例就是PHP递归返回值出现问题时的具体解决方法。