
08-21-2007, 01:28 AM
|
| D-Web Trainee | | Join Date: Feb 2007
Posts: 25
| |
Re: Website Performance Tips & Tricks Use output buffering
This will speed up your PHP code by 5-15% if you frequently print or echo in your code. When output buffering is enabled it pipes the output into a dynamically growing buffer and sends the buffer content with the output in one shot at the end of the script (or when you decide). Output buffering reduces networking overhead substantially at the cost of more memory and an increase in latency.
These are the main functions to control output buffering: * ob_start() enables output buffering. (Output buffering supports multiple levels, that means you can call ob_start() several times.) * ob_end_flush() sends the output buffer and disables output buffering. * ob_end_clean() cleans the output buffer without sending it and disables output buffering. * ob_get_contents() returns the current output buffer as a string that you can echo to send the accumulated output to the browser (after turning buffering off). * int ob_get_length() returns the length of the output buffer.
An example: PHP Code: <?php
ob_start(); //start buffering
ob_implicit_flush(0); //turn off implicit flushing
//now your output stuff:
echo 'This is the 1st line with compressed output.';
$str='<br /> This is the 2nd line.';
echo $str;
$var1=10;
$var2=7;
$var3=$var1-$var2;
echo '<br /> This is the ',$var3, 'rd line.';
$contents = ob_get_contents(); //if you don't add "ob_end_clean()" all that you
wanted to echo out will be echoed out NOW
ob_end_clean(); //if you add this line nothing will be echoed out, only if you add after this line "echo $contents" the content will be echoed out.
?> If your version of PHP is compiled with zlib support and is 4.0.4 or higher, you can also try this: PHP Code: <?php
ob_start('ob_gzhandler');
// rest of your script
?>
Thanks -Kathir |