PHP经过长时间的发展,很多用户都很了解PHP了,PHP最初是1994年Rasmus Lerdorf创建的,刚刚开始只是一个简单的用Perl语言编写的程序,用来统计他自己网站的访问者。后来又用C语言重新编写,包括可以访问数据库。
在我的之前的编程中遇到了一个PHP递归函数的问题,其实是一个很简单的问题。问题出在PHP递归函数的返回值上。这是开始写的:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""><span>functiontest($i) </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">i-</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">4</font></span><span>; </span> </li> <li class="alt"> <span>if($i</span><strong><font color="#006699"><span class="tag"><span class="tag-name">3</span></span></font></strong><span>) </span> </li> <li class=""><span>{ </span></li> <li class="alt"><span>return$i; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>else </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>test($i); </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>echotest(30); </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
这段代码看起来没有问题,其实有else里面是有问题的。在这里执行的test没有返回值。所以虽然满足条件$i
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""><span>functiontest($i) </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">i-</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">4</font></span><span>; </span> </li> <li class="alt"> <span>if($i</span><strong><font color="#006699"><span class="tag"><span class="tag-name">3</span></span></font></strong><span>) </span> </li> <li class=""><span>{ </span></li> <li class="alt"><span>return$i; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>else </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>returntest($i);//增加return,让函数返回值 </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>echotest(30); </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>