phpoptimizationob-start

using of ob_start() is suboptimal (not optimized) and it fills the ram?


Using an output buffer requires the server to store the entire output of the PHP in RAM, so if I have a large page, I'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. that's right ?

I don't want to know the advantage of using ob_start();. My problem is redirecting and this error: Headers already sent.

So for solving that problem, I used of ob_start(); in the fist of my codes. something like this:

<?php ob_start(); ?> 
<?php
       // 500 lines of code is here
       header(Location: www.example.com/test.php);
?> 
<html> 
      // 1000 lines of code is here
</html> 
<?php ob_end_flush(); ?>

Now my problem has been solved, just I want to know everything is ok ? my codes are optimized ? If my requests rise, my site does not delay ?

thanks


Solution

  • Output buffering is frequently used and I wouldn't worry about this. For example, this SO webpage takes up ~ 64 KiB, meaning 16384 of these pages fit in 1 GiB ram simultaneously.

    Probably offtopic, but if you're going to send a Location header, do you even need to execute all the other code? You could just send the header and exit() immediately.