Output buffering in included file [message #178722] |
Tue, 24 July 2012 20:07 |
|
Hello everybody,
instead of using (e.g. in index.php)
<?php
ob_start();
include('included-file.php');
$output = ob_get_clean();
echo $output;
?>
to store the output buffer into a variable, can I use in included-file.php
<?php
ob_start();
// file content
ob_end_flush();
?>
and in index.php
<?php
ob_start();
include('included-file.php');
?>
?
Thank you :)
Best Regards,
Daniel R.
|
|
|
Re: Output buffering in included file [message #178723 is a reply to message #178722] |
Tue, 24 July 2012 20:42 |
Peter H. Coffin
Messages: 245 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Tue, 24 Jul 2012 13:07:20 -0700 (PDT), Dani wrote:
> Hello everybody,
> instead of using (e.g. in index.php)
>
> <?php
> ob_start();
> include('included-file.php');
> $output = ob_get_clean();
> echo $output;
> ?>
>
> to store the output buffer into a variable, can I use in included-file.php
>
> <?php
> ob_start();
> // file content
> ob_end_flush();
> ?>
>
> and in index.php
>
> <?php
> ob_start();
> include('included-file.php');
> ?>
>
> ?
Did you try it? Did it work?
--
You can lead an idiot to knowledge but you cannot make him think. You
can, however, rectally insert the information, printed on stone
tablets, using a sharpened poker.
-- Nicolai
|
|
|
Re: Output buffering in included file [message #178724 is a reply to message #178722] |
Wed, 25 July 2012 05:33 |
J.O. Aho
Messages: 194 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
Dani wrote:
> Hello everybody,
> instead of using (e.g. in index.php)
>
> <?php
> ob_start();
> include('included-file.php');
> $output = ob_get_clean();
> echo $output;
> ?>
>
> to store the output buffer into a variable, can I use in included-file.php
>
> <?php
> ob_start();
> include('included-file.php');
> ?>
You can do all of it in the include file, but everything which is before/after
the include file will not be in the buffer.
Remember also that pages which takes long time to generate, may cause time
outs when using buffers. IMHO don't use buffers if you really don't have a
good reason to use them, just see to that you have proper error handling and
your functions returns the generated content instead of using echo/print.
Those times you have a area where you know there will be issues of long
execution time, just see to generate some output once in a while.
--
//Aho
|
|
|
|
Re: Output buffering in included file [message #178726 is a reply to message #178724] |
Wed, 25 July 2012 09:41 |
|
> You can do all of it in the include file, but everything which is before/after
> the include file will not be in the buffer.
But what is before/after the include file, shouldn't it be included in the buffer started with ob_start(); in index.php?
Thank you :)
Best Regards,
Daniel R.
|
|
|
Re: Output buffering in included file [message #178727 is a reply to message #178722] |
Wed, 25 July 2012 09:56 |
Kim Andr Aker
Messages: 17 Registered: September 2010
Karma: 0
|
Junior Member |
|
|
På Tue, 24 Jul 2012 22:07:20 +0200, skrev Dani <dsdani(at)live(dot)com>:
> Hello everybody,
> instead of using (e.g. in index.php)
>
> <?php
> ob_start();
> include('included-file.php');
> $output = ob_get_clean();
> echo $output;
> ?>
>
> to store the output buffer into a variable, can I use in
> included-file.php
>
> <?php
> ob_start();
> // file content
> ob_end_flush();
> ?>
>
> and in index.php
>
> <?php
> ob_start();
> include('included-file.php');
> ?>
As stated on the manual page http://php.net/ob_start
Output buffers are stackable, that is, you may call ob_start() while
another ob_start() is active. Just make sure that you call ob_end_flush()
the appropriate number of times. If multiple output callback functions are
active, output is being filtered sequentially through each of them in
nesting order.
In essence, you have to use one ob_end_flush() for each ob_start() you
have, or an equal number of ob_end_flush() as you have ob_start(). If you
have ob_start() at the start of your main file or included file, you
should also have an ob_end_flush() at the end of that same file.
--
Kim André Akerø
- kimandre(at)NOSPAMbetadome(dot)com
(remove NOSPAM to contact me directly)
|
|
|
Re: Output buffering in included file [message #178728 is a reply to message #178726] |
Wed, 25 July 2012 10:00 |
M. Strobel
Messages: 386 Registered: December 2011
Karma: 0
|
Senior Member |
|
|
Am 25.07.2012 11:41, schrieb Dani:
>
>> You can do all of it in the include file, but everything which is before/after
>> the include file will not be in the buffer.
>
> But what is before/after the include file, shouldn't it be included in the buffer started with ob_start(); in index.php?
>
> Thank you :)
>
You should rethink it.
Why have an include file echo-ing or print-ing, catch the output in the same file
just to delete it?
Your include files should not output anything while included, this is wrong design.
If the problem is "blank lines or newlines" in the output, consider leaving off the
closing tag ?>, this is standard in Zend programming.
/Str.
|
|
|
Re: Output buffering in included file [message #178729 is a reply to message #178722] |
Wed, 25 July 2012 12:39 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 7/24/2012 4:07 PM, Dani wrote:
> Hello everybody,
> instead of using (e.g. in index.php)
>
> <?php
> ob_start();
> include('included-file.php');
> $output = ob_get_clean();
> echo $output;
> ?>
>
> to store the output buffer into a variable, can I use in included-file.php
>
> <?php
> ob_start();
> // file content
> ob_end_flush();
> ?>
>
> and in index.php
>
> <?php
> ob_start();
> include('included-file.php');
> ?>
>
> ?
>
> Thank you :)
>
Why do you think you need to do this?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
|
Re: Output buffering in included file [message #178732 is a reply to message #178731] |
Wed, 25 July 2012 15:51 |
J.O. Aho
Messages: 194 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 25/07/12 15:31, Dani wrote:
> Jerry Stuckle wrote:
>> Why do you think you need to do this?
>
> To speed up the page :)
And how would it do that with catching the output to a buffer and then
show the buffer content, just simple 1+1 you will have spent more CPU
cycles for the same output as you would have without the buffer.
--
//Aho
|
|
|
Re: Output buffering in included file [message #178733 is a reply to message #178731] |
Wed, 25 July 2012 15:43 |
Peter H. Coffin
Messages: 245 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Wed, 25 Jul 2012 06:31:46 -0700 (PDT), Dani wrote:
> Jerry Stuckle wrote:
>> Why do you think you need to do this?
>
> To speed up the page :)
How slow is the page? (In general, trying to change a working technique
to solve problems that don't exist isn't worth the time and effort.
Speeding up things that are being served adequately or could be 95%
solved by better caching instead of overhauling code usually isn't worth
it.)
--
51. If one of my dungeon guards begins expressing concern over the
conditions in the beautiful princess' cell, I will immediately
transfer him to a less people-oriented position.
--Peter Anspach's list of things to do as an Evil Overlord
|
|
|
Re: Output buffering in included file [message #178734 is a reply to message #178733] |
Wed, 25 July 2012 16:44 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma: 0
|
Senior Member |
|
|
Peter H. Coffin wrote:
> On Wed, 25 Jul 2012 06:31:46 -0700 (PDT), Dani wrote:
>> Jerry Stuckle wrote:
>>> Why do you think you need to do this?
>>
>> To speed up the page :)
>
> How slow is the page? (In general, trying to change a working technique
> to solve problems that don't exist isn't worth the time and effort.
> Speeding up things that are being served adequately or could be 95%
> solved by better caching instead of overhauling code usually isn't worth
> it.)
ACK. On an additional note, Souders claims that most of the time spent
loading a website (80% to 90%) is spent *after* the markup has been loaded.
[1, 2] If that is true, and there is strong indication that it is, then it
is the client side where you can optimize the most, although server-side
optimization certainly should not be neglected.
PointedEars
___________
[1] Souders, Steve (2008): High Performance Websites [Hardcover]. O'Reilly.
ISBN-13: 978-3-89721-850-5.
[2] Souders, Steve (2009): Even Faster Web Sites [Paperback]. O'Reilly.
ISBN-13: 978-0-596-52230-8.
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(at)news(dot)demon(dot)co(dot)uk> (2004)
|
|
|