Home  >  Article  >  Backend Development  >  Sharing solutions to problems that arise when PHP returns values ​​recursively_PHP Tutorial

Sharing solutions to problems that arise when PHP returns values ​​recursively_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:30:39919browse

We are using This is what we started writing:

  1. < ?php
  2. function test($i)
  3. {
  4. $i -= 4;
  5. if($i < 3)
  6. {
  7. return $i;
  8. }
  9. else
  10. {
  11. test($i);
  12. }
  13. }
  14. echo test(30);
  15. ?>

This code looks fine, but in fact there is an else There is something wrong inside. The test executed here has no return value. Therefore, even if the condition $i

<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php  </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><span class="tag-name">3</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></span></li></ol>

The above code example is the specific solution when there is a problem with PHP's recursive return value.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446281.htmlTechArticleWe are using this to start writing: ?php functiontest($i) { $i - = 4; if ($i 3 ) { return$i; } else { test($i); } } echotest(30); ? This code seems to have no problem, but in fact...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn