|
|
LXXV. Output Control FunctionsIntroductie
The Output Control functions allow you to control when output is
sent from the script. This can be useful in several different
situations, especially if you need to send headers to the browser
after your script has began outputting data. The Output Control
functions do not affect headers sent using
header() or setcookie(),
only functions such as echo() and data between
blocks of PHP code.
AfhankelijkhedenDeze functies zijn beschikbaar als onderdeel van
de standarad module die altijd beschikbaar is. InstallatieEr zijn geen handelingen nodig m.b.t. tot installatie
voor deze functies, deze maken deel uit van de kern van PHP. Configuratie tijdens scriptuitvoer
Het gedrag van deze functies wordt be�nvloed vanuit php.ini.
Tabel 1. Output Control configuration options Name | Default | Changeable |
---|
output_buffering | "0" | PHP_INI_PERDIR|PHP_INI_SYSTEM | output_handler | NULL | PHP_INI_PERDIR|PHP_INI_SYSTEM | implicit_flush | "0" | PHP_INI_PERDIR|PHP_INI_SYSTEM |
For further details and definition of the PHP_INI_* constants see
ini_set().
Here is a short explanation of the configuration directives.
- output_buffering
boolean/integer
You can enable output buffering for all files by setting this directive
to 'On'. If you wish to limit the size of the buffer to a certain size -
you can use a maximum number of bytes instead of 'On', as a value for
this directive (e.g., output_buffering=4096).
- output_handler
string
You can redirect all of the output of your scripts to a function. For
example, if you set output_handler to
mb_output_handler(), character encoding will be
transparently converted to the specified encoding. Setting any output
handler automatically turns on output buffering.
- implicit_flush
boolean
FALSE by default. Changing this to TRUE tells PHP to tell the
output layer to flush itself automatically after every output block.
This is equivalent to calling the PHP function
flush() after each and every call to
print() or echo() and each and
every HTML block.
When using PHP within an web environment, turning
this option on has serious performance implications and is generally
recommended for debugging purposes only. This value defaults to
TRUE when operating under the CLI SAPI.
See also ob_implicit_flush().
Resource typesDeze extensie maakt geen gebruik van resources. Voorgedefineerde constantenDeze extensie definieert geen constanten. Voorbeelden
Voorbeeld 1. Output Control example <?php
ob_start();
echo "Hello\n";
setcookie ("cookiename", "cookiedata");
ob_end_flush();
?> |
|
In the above example, the output from echo()
would be stored in the output buffer until
ob_end_flush() was called. In the mean time,
the call to setcookie() successfully stored a
cookie without causing an error. (You can not normally send
headers to the browser after data has already been sent.)
Opmerking:
When upgrading from PHP 4.1 (and 4.2) to 4.3 that due to a bug in
earlier versions you must ensure that
implict_flush is OFF in
your php.ini, otherwise any output with
ob_start() will be not be hidden from output.
User Contributed Notes Output Control Functions |
add a note |
philip at thepr()jects dot ()rg
08-Feb-2001 06:17 |
|
A few tutorials exist on this subject :
*
*
|
|
fredrik at rambris dot com
18-Feb-2002 01:53 |
|
A cool way to use this is to run the output through HTML-tidy and get
really nicelooking output without having to manually add indenting etc.
Try this at the end (after doing an ob_start before outputting
anything).
<?php $str=addslashes(ob_get_contents()); $fp=popen("echo
\"" . $str . "\" | /usr/bin/tidy -i -u -q -latin1
--indent-spaces 1 -wrap 0", "r"); @$newstr=fread($fp,
99999); ob_end_clean(); Header( "Content-length: " .
strlen( $newstr ) ); echo $newstr; ?>
|
|
astericss60 at hotmail dot com
06-Apr-2002 04:25 |
|
You can use the ob_*-functions to replace the PHP variables in a file with
its actual content. It is useful when you have a template file which
includes some variables, like "User=$user" a.s.o. (Probably
there is an easier way?):
$sf = fopen($sourcefile,
"r"); $df = fopen($destfile,
"w");
ob_start(); while( !feof($sf) ) { $line =
fgets($sf, 4096); $eval = "echo
\"".addslashes($line)."\";"; eval($eval); $line
= ob_get_contents(); fputs($df,
stripslashes($line)); } ob_end_clean();
Possibly, it helps
someone...
|
|
nathan at windsofstorm dot net
10-Jul-2002 08:13 |
|
It would seem to me that you could get what you wanted in that manner using
object buffering and the readfile() function...
|
|
18-Nov-2002 04:40 |
|
Wouldn't this be easier?
$sf = fopen($sourcefile,
"r"); $df = fopen($destfile,
"w");
ob_start(); while( !feof($sf) ) { $line =
fgets($sf, 4096); eval("$line =
\"".addslashes($line)."\";"); fputs($df,
stripslashes($line)); } ob_end_clean();
|
|
cnc2dude at earthlink dot net
16-Dec-2002 09:57 |
|
Or just skip output buffering altogether and put:
$parsedtemplate =
eval('return "' . addslashes(file_get_contents('templatefile.html'))
. '";');
|
|
add a note |
| |