output buffering

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

output buffering

Post by vigge89 »

I've just looked trough the output-buffering function at php.net, and i got a question. I'm currently using my own template-system for my site, which works, but it's using variables to store the output before echoing it out. i thought that i could use output-buffering instead, so i wouldn't have to "add contents to the output-variable", but instead, echo it, and then use ob_get_contents() and then ob_end_clean() to get the output, put it into a variable, and then echo it. But now onto the question: Would it be better to use the output-buffering, or should i stick to the variable-based system?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

A matter of taste (?).
I prefeer ob_* when dealing with alot of header() functions as those are being sendt before any other output. Sometimes it's easier to define a header way down the script(s) that "at the top" as usually recommended. Functionallity might be the same, but you have another way to actually write clean, readable code.

But if it's better than any other way, I'm not sure.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

i'll add a question:
is it fast enough, or does it slow down the execution much (noteable times)?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Don't quote me but I think I remember that it is quicker to build strings rather than have many echo statements (buffered or not). I'd have to check my notes which I don't have with me right now.

A possible exception is a site search where I might print each result as it is defined - might be slightly slower overall but, to the user, the script starts providing output more quickly.

Why don't you try a test? There's an example getMicroTime fn in the manual. As with the vast majority of optimisation issues, it's unlikely to make a material difference one way or the other but it's always worth testing.

As Jam mentioned, avoid client output until you're sure all header calls, session_start etc have been carried out.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

yeah, i'll try a test, just give me some mins... ;)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Used this code:

Code: Select all

<?php

##### execution time starts #####
function utime () {
	$time = explode( " ", microtime());
	$usec = (double)$time[0];
	$sec = (double)$time[1];
	return $sec + $usec;
}
$start_time = utime();
#### /execution time starts #####

if ($_GET['i'] == 'ob') {
########### output buffering ###########
ob_start();

### create some output
echo "Output-buffering example<br />"; 

for ($i = 1; $i <= 100; $i++) {
   echo "<br />$i";
}

# get output
$output = ob_get_contents ();

ob_end_clean();

### final output
echo "<html>
<head>
<title>title</title>
</head>
<body bgcolor='#ABCDEF'>

{$output}
";
### /final output
########## /output buffering ###########
}

else {
########### variable style ###########
### create some output
$output = "Variable example<br />"; 

for ($i = 1; $i <= 100; $i++) {
   $output .= "<br />$i";
}

### final output
echo "<html>
<head>
<title>title</title>
</head>
<body bgcolor='#ABCDEF'>

{$output}
";
### /final output
########## /variable style ###########
}


##### output execution time #####
echo "<br /><br />executed in: ".substr(utime () - $start_time, 0, ." seconds";

?>
to test the times, tell me if I should edit anything to get more accurate times. I've tested the code on my own server.

OB-test produced:
0.000367 s
0.000449 s
0.000447 s
0.000436 s
0.000423 s

Variable-test produced:
0.000451 s
0.000488 s
0.000505 s
0.000499 s
0.000468 s

It doesn't seem to be a big difference...

Edit: did a test with a for-loop of 500, this is the results:

OB-test produced:
0.001823 s
0.001125 s
0.001815 s
0.001843 s
0.001955 s

Variable-test produced:
0.002362 s
0.002027 s
0.002171 s
0.001209 s
0.002161 s

Those are some more accurate answers, and the result is that OB seems to be faster...
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

I use a method similar to that which you are describing. It uses ob to get the template files into memory, and is very fast. I understand where your concerns are coming from, but rest assured PHP is very nippy in this area, and shouldn't cause you any problems at all. :)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Sun Apr 18 :? :wink:
Post Reply