Ob_start()

What is the use of ob_start() in PHP?

Questions by raghaven

Showing Answers 1 - 24 of 24 Answers

jwali

  • Apr 6th, 2010
 

Actually, ob_start() tells PHP to start output buffering, meaning that anything you output after ob_start() wont be sent to the browser until the end of the page or ob_end() etc are reached.

So that means that once you use ob_start() and echo something out, you can still set cookies without any errors. For example:

Code
  1. <?php

  2.  


  3. echo "this will work";

  4. setcookie("test", true);

  5.  


  6.  

  7. ?>

$_COOKIES, sets the header. If you do place a ob_start before your cookie call it should stop the header being sent out, which will stop 'header altready sent 'message from apearing.

here are some functions/variables that set the header.

Code
  1. $_COOKIE


ob_start() is used to turn on output buffering.When output buffering is on any output sent from script will be stored into buffer also eliminating header problems which are generated when output is sent from script before  calling header()  function.

  Was this answer useful?  Yes

bibhuphp

  • Jul 22nd, 2010
 

The PHP output buffering will save all the server outputs ( html and php prints) to a string variable. So to start buffering, use ob_start(); this will keep saved any output. Then you use $variable = ob_get_clean(); to stop buffering, and copy the buffer content to the variable.
Here are few samples of the use of ob_start() and ob_get_clean()

Code
  1. <?php ob_start(); //Turn on output buffering ?>

  2. Hello world, <a href="[URL]">link</a>

  3. <div class="style">Content</div>

  4. <?php $var = ob_get_clean(); //copy current buffer contents into $var variable and delete current output buffer ?>


Now the content of the variable $var should be:

Code
  1. Hello world, <a href="[URL]">link</a>

  2. <div class="style">Content</div>

  Was this answer useful?  Yes

rajanbhanot

  • Oct 15th, 2010
 

Turn on Output Buffering.

While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.
Let's consider following example:

Code
  1. <?php

  2. function callbackrajan($buffer)

  3. {

  4.   // replace all the apples with oranges

  5.   return (str_replace("apples", "oranges", $buffer));

  6. }

  7. ob_start("callbackrajan");

  8. ?>

  9. <html>

  10. <body>

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

  12. </body>

  13. </html>

  14. <?php

  15. ob_end_flush();

  16. echo "apples";

  17. ?>


In this all apples will be replaced with oranges leaving last one. Because last echo has been placed after ob_end_flush(). callbackrajan function will be called just after ob_end_flush using all the buffer and will return new buffer which will be redirected to browser.

  Was this answer useful?  Yes

This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

  Was this answer useful?  Yes

abc

  • Sep 12th, 2011
 

it starts object buffering

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions