Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #6649 > unrolled thread

Output buffering in included file

Started byDani <dsdani@live.com>
First post2012-07-24 13:07 -0700
Last post2012-07-25 18:44 +0200
Articles 12 — 7 participants

Back to article view | Back to comp.lang.php


Contents

  Output buffering in included file Dani <dsdani@live.com> - 2012-07-24 13:07 -0700
    Re: Output buffering in included file "Peter H. Coffin" <hellsop@ninehells.com> - 2012-07-24 15:42 -0500
      Re: Output buffering in included file Dani <dsdani@live.com> - 2012-07-25 02:31 -0700
    Re: Output buffering in included file "J.O. Aho" <user@example.net> - 2012-07-25 07:33 +0200
      Re: Output buffering in included file Dani <dsdani@live.com> - 2012-07-25 02:41 -0700
        Re: Output buffering in included file "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-07-25 12:00 +0200
    Re: Output buffering in included file Kim André Akerø <kimandre@nospambetadome.com> - 2012-07-25 11:56 +0200
    Re: Output buffering in included file Jerry Stuckle <jstucklex@attglobal.net> - 2012-07-25 08:39 -0400
      Re: Output buffering in included file Dani <dsdani@live.com> - 2012-07-25 06:31 -0700
        Re: Output buffering in included file "J.O. Aho" <user@example.net> - 2012-07-25 17:51 +0200
        Re: Output buffering in included file "Peter H. Coffin" <hellsop@ninehells.com> - 2012-07-25 10:43 -0500
          Re: Output buffering in included file Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-07-25 18:44 +0200

#6649 — Output buffering in included file

FromDani <dsdani@live.com>
Date2012-07-24 13:07 -0700
SubjectOutput buffering in included file
Message-ID<61c7743a-3a40-4465-b8a0-47acb31b6628@googlegroups.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');
?>

?

Thank you :)

[toc] | [next] | [standalone]


#6650

From"Peter H. Coffin" <hellsop@ninehells.com>
Date2012-07-24 15:42 -0500
Message-ID<slrnk0u267.h39.hellsop@nibelheim.ninehells.com>
In reply to#6649
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

[toc] | [prev] | [next] | [standalone]


#6652

FromDani <dsdani@live.com>
Date2012-07-25 02:31 -0700
Message-ID<4306d71a-3478-4750-8740-f050ca8aecde@googlegroups.com>
In reply to#6650
> Did you try it? Did it work?
> 
It seems to work because if I use ob_end_clean() instead of ob_end_flush() in the included file, the content of the included file is not displayed in index.php.

[toc] | [prev] | [next] | [standalone]


#6651

From"J.O. Aho" <user@example.net>
Date2012-07-25 07:33 +0200
Message-ID<a79eojFnn7U1@mid.individual.net>
In reply to#6649
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

[toc] | [prev] | [next] | [standalone]


#6653

FromDani <dsdani@live.com>
Date2012-07-25 02:41 -0700
Message-ID<e86f62ea-24bf-4f41-b0a4-e496dbc60175@googlegroups.com>
In reply to#6651
> 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 :)

[toc] | [prev] | [next] | [standalone]


#6655

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-07-25 12:00 +0200
Message-ID<a79uepF914U1@mid.uni-berlin.de>
In reply to#6653
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.

[toc] | [prev] | [next] | [standalone]


#6654

FromKim André Akerø <kimandre@nospambetadome.com>
Date2012-07-25 11:56 +0200
Message-ID<op.whzxwgioptrp3x@neonk2.intrahouse.local>
In reply to#6649
På Tue, 24 Jul 2012 22:07:20 +0200, skrev Dani <dsdani@live.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@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)

[toc] | [prev] | [next] | [standalone]


#6656

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-07-25 08:39 -0400
Message-ID<juopej$mvv$1@dont-email.me>
In reply to#6649
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#6658

FromDani <dsdani@live.com>
Date2012-07-25 06:31 -0700
Message-ID<a717d638-6b82-4e80-9421-8fd9b631d555@googlegroups.com>
In reply to#6656
Jerry Stuckle wrote:
> Why do you think you need to do this?

To speed up the page :)

[toc] | [prev] | [next] | [standalone]


#6659

From"J.O. Aho" <user@example.net>
Date2012-07-25 17:51 +0200
Message-ID<a7aiv7F233U1@mid.individual.net>
In reply to#6658
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

[toc] | [prev] | [next] | [standalone]


#6660

From"Peter H. Coffin" <hellsop@ninehells.com>
Date2012-07-25 10:43 -0500
Message-ID<slrnk1051u.h39.hellsop@nibelheim.ninehells.com>
In reply to#6658
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

[toc] | [prev] | [next] | [standalone]


#6661

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-07-25 18:44 +0200
Message-ID<1853417.E1bRvOsfjV@PointedEars.de>
In reply to#6660
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@news.demon.co.uk> (2004)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web