In PHP, the output buffering mechanism is used to cache output generated by scripts until the script has finished executing, at which point the content is sent to the browser. This provides two main advantages:
It allows you to control when output is sent, enabling certain processing during page rendering.
It can improve performance in some scenarios by reducing the number of interactions with the browser.
ob_flush() is an output buffer function that sends the contents of the output buffer to the browser, but it does not clear the buffer. So after calling ob_flush(), the buffer's content remains available for further use.
flush() is used to force a flush of the system output buffer. It tries to send all data to the browser, but unlike ob_flush(), it doesn’t interact with PHP’s output buffer—only the internal system buffer. When flush() is called, PHP pushes the buffer content to the browser for the user to see immediately.
While both ob_flush() and flush() can send output to the browser, they serve different purposes and behave differently in various contexts. Here's a detailed comparison:
Feature | ob_flush() | flush() |
---|---|---|
Target Buffer | PHP output buffer | System output buffer |
Clears Buffer | No | Yes |
Depends on Output Buffering | Yes | No |
Common Use Cases | Output buffer control, usually with ob_start() | Force immediate browser output, often for long-running scripts |
ob_flush() is used when PHP output buffering is enabled, and it sends the current buffer content to the browser without clearing it.
flush() is a system-level output flush function that sends content to the browser without relying on PHP's output buffering.
<?php
// Start output buffering
ob_start();
<p>echo "First chunk of data being prepared...";</p>
<p>// Send buffer content to the browser but retain it<br>
ob_flush();<br>
flush();</p>
<p>echo "Second chunk of data, shown immediately to the user.";</p>
<p>// End script and flush remaining buffer<br>
ob_end_flush();<br>
?>
In this example, ob_start() enables output buffering. ob_flush() sends the current buffer contents to the browser but retains the buffer. Therefore, the second echo is also buffered until the script ends.
<?php
echo "Starting data processing...";
<p>// Force flush of output<br>
flush();</p>
<p>// Simulate a long-running task<br>
sleep(2);<br>
echo "Task completed.";<br>
?>
Here, flush() forces the browser to display "Starting data processing..." immediately, while "Task completed." appears after a 2-second delay. This is especially useful when users need real-time progress feedback.
Long-running scripts (e.g., downloads or file processing)
If you're running tasks that take significant time (such as file uploads or downloads), using ob_flush() and flush() helps show progress to the user and prevents browser timeouts.
Real-time data pushing
For use cases like real-time dashboards or chat systems, flush() ensures data is pushed to the browser immediately, avoiding delays or blank pages.
Page rendering optimization
During page rendering, combining ob_flush() and flush() allows partial content to be shown to users while the script is still running, improving user experience.