Viewed   92 times

What is output buffering and why is one using it in PHP?



Output Buffering for Web Developers, a Beginner’s Guide:

Without output buffering (the default), your HTML is sent to the browser in pieces as PHP processes through your script. With output buffering, your HTML is stored in a variable and sent to the browser as one piece at the end of your script.

Advantages of output buffering for Web developers

  • Turning on output buffering alone decreases the amount of time it takes to download and render our HTML because it's not being sent to the browser in pieces as PHP processes the HTML.
  • All the fancy stuff we can do with PHP strings, we can now do with our whole HTML page as one variable.
  • If you've ever encountered the message "Warning: Cannot modify header information - headers already sent by (output)" while setting cookies, you'll be happy to know that output buffering is your answer.
Thursday, August 11, 2022

You can check any INI setting in PHP with the ini_get method.


Likewise, you can change most INI settings with ini_set:

ini_set('output_buffering', 'on');
Tuesday, October 11, 2022

A little known feature of PHP is being able to treat an included/required file like a function call, with a return value.

For example:

// myinclude.php
$value = 'foo';
$otherValue = 'bar';
return $value . $otherValue;

// index.php
$output = include './myinclude.php';
echo $output;
// Will echo foobar
Monday, October 3, 2022

Probably you are using a buffering function in output buffering callback which isn't possible as mentioned in php ob_start output_callback documentation. If not it should be the output-handler you used, check your php.ini and try to set it's value to "none" if possible.

Tuesday, October 4, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :