Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes calling ob_end_clean() once at the top of a script guarantee ob_flush() is never needed?
    primarykey
    data
    text
    <p>When using PHP as the back-end for SSE (Server Sent Events) and similar server streaming solutions, I have been using the <code>@ob_flush();@flush()</code> idiom to make sure the data gets spat out immediately. (The ordering is important: <code>ob_flush()</code> flushes PHP's buffers, <code>flush()</code> then tells Apache to flush it too.)</p> <p>I started thinking about it after reading that <code>@</code> is slow. (In fact, according to comments in the PHP manual it adds roughly 0.005ms, so I've reclassified "slow" as "don't worry about it unless inside a tight loop"). I need the <code>@</code> on <code>ob_flush()</code> to stop it complaining when the output buffer is empty. BTW, I've followed through the source code until it reached Apache and it appears <code>flush()</code> does not throw any PHP errors.</p> <p>Now I'm wondering if I can reduce the above <code>@ob_flush();@flush()</code> to just <code>@flush()</code> (or even just <code>flush()</code>)? Again in PHP manual comments I find someone saying you could do this at the top of your script: <code>if (ob_get_level()) ob_end_clean();</code> (Which can be abbreviated to simply <code>@ob_end_clean();</code>)</p> <p>My question is: Is that a reliable command to mean output buffering will never be used and <code>ob_flush()</code> will never be needed? Across all PHP versions (well at least php 5.2 onwards) and all supported operating systems, with whatever weird settings a user might have put in php.ini?</p> <p>(If your answer is "no", please describe under which situations <code>ob_flush()</code> could still be needed.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload