Jump to content

Purpose of ob_start ?


f1r3fl3x

Recommended Posts

Y'know, I would like to know this too, I have always wondered why people do this, AIK is that it is meant to make the page loads quicker as it is an output buffer, but if someone out there could explain it a little more eloquently it would be much appreciated.  It's something that I should read up on but never had the need to; kinda like regexp, which I REALLY had ought to sit down for an afternoon and LEARN.

 

Cheers,

Rw

Link to comment
Share on other sites

There are many reasons.  Some people do it to buffer any output so that if they need to send headers, redirect then they can just throw away the output.  This is probably due to bad design.

 

Another reason may be to get some output for later use, like if you include a file that outputs something you can store it in a var for manipulation or later use.

Link to comment
Share on other sites

Y'know, I would like to know this too, I have always wondered why people do this, AIK is that it is meant to make the page loads quicker as it is an output buffer, but if someone out there could explain it a little more eloquently it would be much appreciated.  It's something that I should read up on but never had the need to; kinda like regexp, which I REALLY had ought to sit down for an afternoon and LEARN.

 

Cheers,

Rw

 

I read some stuff about this last week because I saw some people around here using it, but the stuff I read didnt convince me really to use it. Nor was it extremely clear why to and why not.

Link to comment
Share on other sites

Y'know, I would like to know this too, I have always wondered why people do this, AIK is that it is meant to make the page loads quicker as it is an output buffer, but if someone out there could explain it a little more eloquently it would be much appreciated.  It's something that I should read up on but never had the need to; kinda like regexp, which I REALLY had ought to sit down for an afternoon and LEARN.

 

Cheers,

Rw

 

I read some stuff about this last week because I saw some people around here using it, but the stuff I read didnt convince me really to use it. Nor was it extremely clear why to and why not.

Can you give us a link to that article :)

Link to comment
Share on other sites

Output Buffering is useful if:

 

1. You still want to send headers while output may exist

2. If you want to take advantage of ob_get_level() and thus nesting output (for example load all modules of a page before you load the main page)

3. You want to compress the output with g-zip ob_start('ob_gzhandler');

4. Output has to go to more then one destination (screen, cache, ftp, ..)

 

There are many uses for ob_ find one ;)

Link to comment
Share on other sites

I've always used it heavily in MVC-style setups.  With the output buffer functions you can process and render a portion of the page within its own object before a piece of the template or view has already been rendered.  This can help you to set up a dynamic templating system where you can just modify the database to change page layouts.  It also allows me to perform post-processing on a rendered view, such as applying attributes, styles, or converting links.

 

There have also been times where some framework like CakePHP is just misbehaving and is throwing output like newlines when you need to modify a header sometime later in your code, in which you can call ob_clean() (without first doing an ob_start(), so it's performed on the standard output) before modifying the header to fix the problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.