Search code examples
phpoutput-buffering

How can an output buffer worsen performance


I am writing a php script and somewhere before my header() function i have printed text to the browser hereby causing my header() function give me a well known error:

Warning: Cannot modify header information - headers already sent.

Now my question is, I have the intentions of using ob_start() and ob_flush() before and after the header() function. But I once heard something like output buffer can affect the performance of one's application negatively. How true is that?

Or should I just stick with the idea of printing Javascript function to redirect the page.

Thanks for your time.


Solution

  • Using an output buffer requires the server to store the entire output of the PHP in RAM, so if it's a large page, you'll wind up using a fair amount of memory - and the server will also have to wait until the entire page is generated before sending it out, which could cause a small delay. But other than that, I don't think there's much of a disadvantage in using an output buffer. For what you want to do, it's certainly a reasonable solution.