Jump to content

Output buffering?


Recommended Posts

from php:

 

<?php

function callback($buffer)
{
  // replace all the apples with oranges
  return (str_replace("apples", "oranges", $buffer));
}

ob_start("callback");

?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php

ob_end_flush();

?>

The above example will output:

 

 

<html>

<body>

<p>It's like comparing oranges to oranges.</p>

</body>

</html>

 

Link to comment
https://forums.phpfreaks.com/topic/110012-output-buffering/#findComment-564530
Share on other sites

"how does it work?"

 

When you use output buffering, the result generated by php(usually html) is sent to a buffer (meaning saved in memory)  instead of directly being sent to the browser.  The saved material will wait in the buffer until you tell it go to the browser. 

 

The primary use for output buffering is header redirection.  As you may know that a header resides at the very beginning of a html file and a html file can have only one header.  Therefore, you can't do a header redirection in the middle of a page.  But you can do this if you are using output buffering.  Because the page goes to the buffer and does not get displayed by the browser(using the first header) first. 

 

The bad news is that output buffering taxes the server.  And crappy servers will generate memory errors every now and then.  Hope that makes thing clearer for you. 

Link to comment
https://forums.phpfreaks.com/topic/110012-output-buffering/#findComment-564553
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.