How to disable output buffering in PHP
In PHP, you can disable output buffering by calling the ob_end_flush()
function or flush()
function. ob_end_flush() and flush() are functions in PHP used to control output buffering. ob_end_flush() turns off output buffering and sends the buffered data to the output, while flush() forces PHP to flush the output buffer immediately, sending any buffered data to the output. You can also use the ini_set()
function to set the output_buffering
configuration directive to Off
. For example:
<?php
ob_start();
for ($i = 0; $i < 5; $i++) {
echo "Line $i\n";
ob_flush();
flush();
sleep(1);
}
ob_end_flush();
Watch a video course
Learn object oriented PHP
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
<?php
ini_set('output_buffering', 'Off');
for ($i = 0; $i < 5; $i++) {
echo "Line $i\n";
flush();
sleep(1);
}
This will turn off output buffering for the entire script.