understanding output buffering

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cardi777
Forum Commoner
Posts: 54
Joined: Sun Mar 29, 2009 4:26 am

understanding output buffering

Post by cardi777 »

Hi all. I have been looking into output buffering for speeding up my cms website.

I'm struggling to understand the principles behind it. I read the manual, but i still have questions :)

Say for instance if I have a highly dynamic database driven website where the menu, footer, and content is changing very often on a daily basis. I would have thought that output buffering would cause issues by showing old content instead of the newer. From what I have read, ob doesn't compare data to check for changes, it just does what you tell it. so I think I have misunderstood how it is surposed to work.

Say if I did this...

Code: Select all

<?php
ob_start('ob_gzhandler');
 
all my code here for a dynamic page inc header, content, footer
 
?>
note: i didn't close the ob on purpose

What would that achieve ? Would I need to wrap each dynamic element this like instead...

Code: Select all

<?php
ob_start('ob_gzhandler');
[b]dynamic header code...[/b]
 
ob_start('ob_gzhandler');
[b]dynamic content code...[/b]
ob_end_flush();
  
[b]dynamic footer code...[/b]
ob_end_flush();
?>
Any help would be great!

Cheers,
Doug
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: understanding output buffering

Post by requinix »

I think you're confusing output buffering with caching. They're two completely different concepts.
cardi777
Forum Commoner
Posts: 54
Joined: Sun Mar 29, 2009 4:26 am

Re: understanding output buffering

Post by cardi777 »

i thought something was going on... so whats the difference ?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: understanding output buffering

Post by requinix »

Output buffering is just a way of not displaying something when normally you would. Like, you can turn on output buffering at the start of your script, let it run, then at the end you can get everything outputted as a string.

Code: Select all

ob_start();
echo "This will not be outputted yet.";
$string = ob_get_contents(); // This will not be outputted yet.
ob_end_clean(); // stop output buffering and clear everything collected
 
echo $string; // now you'll see it
Caching is a way of reusing stuff that was generated previously but isn't quite "old" yet. For example, I could have a PHP script that generates an RSS feed, but since it doesn't change much I cache the feed to a file on the server: if the cached version has expired then it's regenerated, then the script outputs it. Thus if the cache is still good the script doesn't have to recreate the feed every single time it's requested.
peterjwest
Forum Commoner
Posts: 63
Joined: Tue Aug 04, 2009 1:06 pm

Re: understanding output buffering

Post by peterjwest »

In addition to what tasairis said, if you wish to cache files you will need to either store them on a database (e.g. mysql) or as files. Your CMS may support caching, otherwise you'll have to write it yourself.

Saving files would probably be easiest. You'll need to use fopen() to open and save files. Essentially you'll want to save an html file for each section and save the date of the cache somehow. Then you need to update the cache when its out of date.
HavokDelta6
Forum Newbie
Posts: 16
Joined: Mon Sep 28, 2009 4:11 pm

Re: understanding output buffering

Post by HavokDelta6 »

aaaaaaaaaaaaaaaaaaaaaaaaaaaa
Last edited by HavokDelta6 on Tue Mar 20, 2012 6:03 am, edited 1 time in total.
cardi777
Forum Commoner
Posts: 54
Joined: Sun Mar 29, 2009 4:26 am

Re: understanding output buffering

Post by cardi777 »

@ tasairis: I think im a little slow, but can you give me a practical example of when this would be useful?
@ peterjwest:I have been using gCache but its not fitting for all occasions. Why would you write cache to a db? Is that faster or something?
@ HavokDelta6: Thanks for that! Looks particularly useful. But I am still struggling to understand the concept/principle although I understand the code. Its very foreign to me still!

I have one more question a little off topic, but how much faster does a website run if you choose better(optimal) db field types? Is it significant?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: understanding output buffering

Post by Eran »

Output buffering gives you better control of output - it allows you to control when and what output is sent (to the browser or otherwise). This is useful when you need to run additional processing on the output before outputting it (most CMS do that).

Optimal database field types can improve performance, but mostly you won't see much difference unless it's very unoptimized. It makes more of a difference for larger databases, where such performance hits become more pronounced.

Unless your website receives heavy traffic or the CMS has some deep performance flaws, caching would probably not make much of a difference. You should look to optimize the client side as there is probably a lot of opportunity for optimization there. http://developer.yahoo.com/performance/rules.html
HavokDelta6
Forum Newbie
Posts: 16
Joined: Mon Sep 28, 2009 4:11 pm

Post by HavokDelta6 »

aaaaaaaaaaaaaaaaaaaaa
Last edited by HavokDelta6 on Tue Mar 20, 2012 5:58 am, edited 1 time in total.
peterjwest
Forum Commoner
Posts: 63
Joined: Tue Aug 04, 2009 1:06 pm

Re: understanding output buffering

Post by peterjwest »

@ cardi777: For some CMS there will be a lot of processing and many database interactions (e.g. 10-20) per request, if you cache the page in the db you can potentially eliminate most of those, however files may serve you just as well.

I agree with pytrin mostly, caching probably won't help you that much. Get a profiler to find out exactly where you need to optimise. Client side optimisation will help your users but not your site.

Another option would be to take features which change infrequently and only update them probabilistically (e.g. one in ten requests) using a random variable. This lessens the load on each request. Just an idea, I have no idea if any real websites do this :P
cardi777
Forum Commoner
Posts: 54
Joined: Sun Mar 29, 2009 4:26 am

Re: understanding output buffering

Post by cardi777 »

thanks guys !
Post Reply