PHP递归函数

PHP还支持递归函数调用, 例如C / C ++。在这种情况下, 我们在函数内调用当前函数。也称为递归。

建议避免递归函数调用超过200个递归级别, 因为它可能会破坏堆栈并可能导致脚本终止。

示例1:打印号码

<?php  
function display($number) {  
    if($number<=5){  
     echo "$number <br/>";  
     display($number+1);  
    }
}  
  
display(1);  
?>

输出

1
2
3
4
5

示例2:阶乘数

<?php  
function factorial($n)  
{  
    if ($n < 0)  
        return -1; /*Wrong value*/  
    if ($n == 0)  
        return 1; /*Terminating condition*/  
    return ($n * factorial ($n -1));  
}  
  
echo factorial(5);  
?>

输出

120

来源:

https://www.srcmini02.com/29381.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37957.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?