duanheyi7147 2014-12-03 06:02
浏览 58
已采纳

致命错误:第15行的C:\ xampp \ htdocs \ main.php中允许的内存大小为134217728字节(试图分配8192字节)

I have Problem with my recursion function. I want to wait until text file input change then redirect to new page.

15 line is

$readedData = fread($myfile,filesize("my1.txt"));

this is my function

first();

function first() {
    $myfile = fopen("my1.txt", "r") or die("Unable to open file!");
    $number = fread($myfile,filesize("my1.txt"));

    fclose($myfile);
    recursion($number);
}

function recursion($number2) { 
    $myfile = fopen("my1.txt", "r") or die("Unable to open file!");
    $readedData = fread($myfile,filesize("my1.txt"));

    echo ($readedData ); 

    fclose($myfile);
    if($number2 != $readedData){
        return (recursion($readedData));
    }
    else if($number2 == $readedData){
        return(first());
    }
}
  • 写回答

3条回答 默认 最新

  • doushan6692 2014-12-03 06:15
    关注

    Yes you do, your recursion is not bounded, it keeps calling deeper and deeper. Waiting for a condition is usually handled with a conditional loop, not self-recursion.

    first() always calls recursion()

    recursion() calls itself, or calls first(). Which always calls recursion().

    You see the problem -- these calls never end, they always call recursion(). Each call allocates stack space to be able to run, until the maximum configured limit is hit (130MB by default, which is what your error reported too)

    Edit: php has no setInterval() like javascript, and in php recursive calls are truly recursive, they do not just queue a continuable. To wait until the file changes in a loop you can do something like:

    function waitAndRedirect( $filename ) {
        waitForchange(file_get_contents($filename));
        handleRedirect();
    }
    
    function waitForChange( $oldData ) {
        while (true) {
            $currentData = file_get_contents($filename);
            if ($currentData !== $oldData) return;
            else usleep(200);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改