How to disable output buffering in PHP
In PHP, you can disable output buffering by calling the ob_end_flush() function or flush() function.
In PHP, output buffering can be controlled or disabled using different functions and configuration directives. Note that ob_end_flush() and flush() do not disable buffering; they only flush or terminate the current buffer level. ob_end_flush() turns off output buffering for the current level and sends the buffered data to the output, while flush() forces PHP to send any currently buffered data to the web server. To completely disable output buffering for a script, you can use the ini_set() function to set the output_buffering configuration directive to 0. For example:
Example of flushing output with ob_end_flush() and flush() in PHP
<?php
ob_start();
for ($i = 0; $i < 5; $i++) {
echo "Line $i\n";
ob_flush();
flush();
sleep(1);
}
ob_end_flush();
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Learn object oriented PHP</div>
Note that this example uses the sleep function to pause the execution of the script for 1 second after each line is output, to simulate a slow output.
Or
Example of using the ini_set() function to set the output_buffering configuration in PHP
<?php
ini_set('output_buffering', '0');
for ($i = 0; $i < 5; $i++) {
echo "Line $i\n";
flush();
sleep(1);
}This will turn off output buffering for the entire script.
Note: Output buffering can be controlled at multiple layers, including PHP, the web server (e.g., Apache's
mod_deflateor Nginx's gzip compression), and the browser. Even with buffering disabled in PHP, server-level compression or buffering may still delay output.