PHP输出缓冲抛出警告

I have to output results while PHP script is running:

ob_implicit_flush(true);
ob_start();
for ($i=0; $i<5; $i++) {
    echo $i . ' - ';
    ob_end_flush();
    flush();
    sleep(1);
}

This is working but throwing warnings:

Notice: ob_end_flush(): failed to delete and flush buffer. No buffer to delete or flush in ...

How to fix that?

In the first loop you are ending the buffer. If you want to do that, you should start a new buffer in each loop.

ob_implicit_flush(true);
for ($i=0; $i<5; $i++) {
    ob_start();
    echo $i . ' - ';
    ob_end_flush();
    flush();
    sleep(1);
}

This is a neater way to use buffering. It is also more performant as you are only using 1 buffer. If you do not need to flush on each loop, you could skip the ob_flush within the loop and use ob_end_flush instead of ob_end_clean.

ob_implicit_flush(true);
ob_start();
for ($i=0; $i<5; $i++) {
    echo $i . ' - ';
    ob_flush();
    flush();
    sleep(1);
}
ob_end_clean();

ob_end_flush() will disable buffering, so future calls to this in your loop will fail. Simply use ob_flush(), which clears the buffer but maintains output buffering:

ob_implicit_flush(true);
ob_start();
for ($i=0; $i<5; $i++) {
    echo $i . ' - ';
    ob_flush();
    sleep(1);
}
ob_end_flush();

Ok, I fixed it like that:

ob_implicit_flush(true);
ob_start();
for ($i=0; $i<5; $i++) {
    echo $i . ' - ';
    flush();
    @ob_flush();
    @ob_end_clean();
    sleep(1);
}

It works now.