Fork me on GitHub

php递归函数

递归函数是PHP中较难掌握的地方,还好并不非常常用,一般在制作无限极栏目分类的时候会使用。

其实所谓递归函数,就是自己调用自己的函数。

  1. function test($n) {
  2. echo $n." ";
  3. if($n > 0) {
  4. test($n-1);
  5. } else {
  6. echo "<-->";
  7. }
  8. echo $n." ";
  9. }
  10. test(10);
<?php
/**
  *php递归函数示例
  *(从1到100的累加和计算)
* */
function summation($number){
  $total = $number;
  if($number >1){
      $total += summation(--$number);
   }
   return $total;
}

echo summation(100);
posted @ 2018-10-26 16:27  big2cat  阅读(241)  评论(0编辑  收藏  举报