版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_36681154/article/details/83219905
求5的阶乘
代码如下:
<?php
function jiecheng($n){
$result = $n*jiecheng($n-1);
return $result;
}
$v1=jiecheng(5);
echo $v1;
?>
出现结果:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65488 bytes) in D:\phpStudy\WWW\php\function\jiecheng.php on line
就是说这个阶乘是一个无限循环从5*4*.....
解决办法:
function jiecheng($n){
if($n==1){//加if限制循环
return 1;
}
$result = $n*jiecheng($n-1);
return $result;
}
$v1=jiecheng(5);
echo $v1;
?>