简体   繁体   中英

output buffering ob_get_clean not working

I have the code below code that works correctly as what i want. It does not send any output on browser that is what i exactly want.

ob_start();
echo "test";
echo "test";
$output = ob_get_clean( );

But problem is in my below code. below code start sending the output on the browser even i have $output = ob_get_clean( ); at the end

ob_start();
for($i=0;$i<=10000000;$i++){
    echo $i."<br/>";
}
$output = ob_get_clean( );

I am unable to understand the concept of output buffering. everyone say you can control the output and send the output when you want but my above script start sending output to the browser.

There's a limit to how much there can be buffered which by default is 4KB so you are hitting the maximum with your script. If you want to use it for a larger buffer you have to edit your php.ini settings to reflect that. To quote from the PHP docs

You can enable output buffering for all files by setting this directive to 'On'. If you wish to limit the size of the buffer to a certain size – you can use a maximum number of bytes instead of 'On', as a value for this directive (eg, output_buffering=4096). As of PHP 4.3.5, this directive is always Off in PHP-CLI. source

The PHP doc is quite technical on the subject so I found this blog post which explains the bits and tricks to output buffering:

Streaming and Output Buffering

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM