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条)

报告相同问题?

悬赏问题

  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1