Search code examples
phpapacheechoflush

How to flush output after each `echo` call?


I have a php script that only produces logs to the client.
When I echo something, I want it to be transferred to client on-the-fly.
(Because while the script is processing, the page is blank)
I had already played around with ob_start() and ob_flush(), but they didn't work.

What's the best solution?

PS: it is a little dirty to put a flush at the end of the echo call...

EDIT: Neither the Answers worked, PHP or Apache Fault?


Solution

  • Edit:

    I was reading the comments on the manual page and came across a bug that states that ob_implicit_flush does not work and the following is a workaround for it:

    ob_end_flush();
    
    # CODE THAT NEEDS IMMEDIATE FLUSHING
    
    ob_start();
    

    If this does not work then what may even be happening is that the client does not receive the packet from the server until the server has built up enough characters to send what it considers a packet worth sending.


    Old Answer:

    You could use ob_implicit_flush which will tell output buffering to turn off buffering for a while:

    ob_implicit_flush(true);
    
    # CODE THAT NEEDS IMMEDIATE FLUSHING
    
    ob_implicit_flush(false);