码迷,mamicode.com
首页 > Web开发 > 详细

PHP - 递归函数

时间:2015-11-22 13:47:17      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

    /**
     * factorial($num) 计算阶乘
     * @param string $num
     * @return string $total
     */
    function factorial($num) {
        if (empty($num)) {
            return ‘输入不能为空!‘;
        }
        elseif ($num < 0){
            echo $num.‘没有阶乘!‘;
        }
        elseif ($num == 0 || $num == 1) {
            echo $num.‘的阶乘等于其本身!‘;
        }
        else {
            static $total = 1;
            $total *= $num;
            $num--;
            if ($num != 1) {
                factorial($num);
            }
            return $total;
        }
    }

 

PHP - 递归函数

标签:

原文地址:http://www.cnblogs.com/KTblog/p/4985573.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!