Search code examples
phpapache2output-buffering

understanding flush,Ob_flush and buffer_output in PHP


i was reading about the buffering of contents and i found a simple script to show effects of flush

<?php

if (ob_get_level() == 0) ob_start();

for ($i = 0; $i < 10; $i++) {

    echo "<br> Line to show.";
    echo str_pad('', 4096) . "\n";

    ob_flush();
    flush();
    sleep(2);
}

echo "Done.";

ob_end_flush();

?>

this script works fine and show the output , but when i remove the str_pad or reduce the length from 4096 to 40 flush does not work.

can any one help me out what exactly causing this..


Solution

  • Atlast i found the reason.

    According to PHP.net Broswers require certain bytes to start the display of the page. Like some version of Internet Explorer require 200 bytes. And modern broswers like Firefox or Chrome requires more bytes to start Browser display.

    In above case if you check it in Internet Explorer it will not require string padding but in Firefox or Chrome you need to Padd spaces with input to display flushed output.