f1r3fl3x Posted September 14, 2010 Share Posted September 14, 2010 Hey guys, what's the purpose of using ob_start() in the begining of the script? I even saw alot of people turning output buffering by default in their php.ini file ! What's the benefit of this action ? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/213413-purpose-of-ob_start/ Share on other sites More sharing options...
rwwd Posted September 14, 2010 Share Posted September 14, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/213413-purpose-of-ob_start/#findComment-1111101 Share on other sites More sharing options...
AbraCadaver Posted September 14, 2010 Share Posted September 14, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/213413-purpose-of-ob_start/#findComment-1111114 Share on other sites More sharing options...
fortnox007 Posted September 14, 2010 Share Posted September 14, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/213413-purpose-of-ob_start/#findComment-1111147 Share on other sites More sharing options...
f1r3fl3x Posted September 15, 2010 Author Share Posted September 15, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/213413-purpose-of-ob_start/#findComment-1111455 Share on other sites More sharing options...
ignace Posted September 15, 2010 Share Posted September 15, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/213413-purpose-of-ob_start/#findComment-1111465 Share on other sites More sharing options...
f1r3fl3x Posted September 16, 2010 Author Share Posted September 16, 2010 @ignace, thanks for the explanation! Can you give an example of pt 2 of yoyr list ? Quote Link to comment https://forums.phpfreaks.com/topic/213413-purpose-of-ob_start/#findComment-1111641 Share on other sites More sharing options...
termidave Posted September 16, 2010 Share Posted September 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/213413-purpose-of-ob_start/#findComment-1111644 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.