What are the pros and cons of output buffering? [duplicate]
Asked Answered
F

2

7

I have read on many websites that using

ob_start(); 

can enhance your page load times, as it stores the php in a variable and displays it in one go rather than processing the php a bit of a time.

Also it is extremely useful for

header('location: /');

Some people say that this is spaghetti code, but as long as the code is clear and concise to any programmer then this should not be a problem, right?

What are your thoughts to using it, and what do you set as your output buffering, are there pros and cons to how, when and why I should or shouldn't use it.

Fado answered 19/4, 2011 at 11:58 Comment(1)
Regarding the second question part: Is it "OK" to have "ob_start()" in the beginning of all PHP pages? - it is widely considered to be hack if its purpose is just to prevent the headers already sent error. But then, it's a functional hack. Just beware that reliability depends on wether PHP implicitly flushes (the default buffer size can depend on php.ini settings).Terraterrace
T
1

The main advantage of output buffering is that you can use it with the ob_gzhandler which will compress your output so you use less bandwidth. Good to use if your server is not setup to send php files compressed.

Another advantage is if your script uses a database or other constrained resources and you have some output before closing your connections or releasing those resources. Instead of having this kind of thing:

  1. Connect to database
  2. Start sending output to the user
  3. Wait for the user to receive everything
  4. Close the database connection

You have:

  1. Start buffering
  2. Connect to database
  3. Output some things
  4. Close database connection
  5. Send the buffer to the user.

When your script would need to be connected for 100ms to the database and your user need 300 more to download it, you can understand how output buffering can help releasing some stress on the database connections limit.

I know something coded well using a well configured server could nullify those advantages, but you never know who will code after you and you don't always have control of the server it's running on.

Tadd answered 19/4, 2011 at 12:29 Comment(0)
B
-1

some user dont know php well. so they use ob_start wrongly.

if you are using header functions such as header(), cookie(), session you dont have to send any output. these function have to use from before output.

but some user is to stop sending output using ob_start or output buffering function.

so you could use javascript or meta forwading to forward user.

<script language="javascript"> window.location = 'some.php'; </script>

or you could use meta refresh to forward user.

<META HTTP-EQUIV="Refresh" CONTENT="0;URL=some.php"> 

if you really need to use header function you must dont send any output(dont forget that enter character or space is or UTF-8 signature is output too)

Babette answered 19/4, 2011 at 12:4 Comment(1)
-1 - It's not wrong to use ob_start as a method of preventing output being sent. However, normally, it could probably impact performance and mean that you have a layering problem in your code.Mella

© 2022 - 2024 — McMap. All rights reserved.