1. 程式人生 > >Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65488 bytes) in 的解決

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65488 bytes) in 的解決

求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;
?>