CIV. Output Control Functions

Введение

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.

Требования

Эти функции всегда доступны.

Установка

Для использования этих функций не требуется проведение установки, поскольку они являются частью ядра PHP.

Настройка во время выполнения

Поведение этих функций зависит от установок в php.ini.

Таблица 1. Output Control configuration options

NameDefaultChangeableChangelog
output_buffering"0"PHP_INI_PERDIR 
output_handlerNULLPHP_INI_PERDIRAvailable since PHP 4.0.4.
implicit_flush"0"PHP_INI_ALLPHP_INI_PERDIR in PHP <= 4.2.3.
Для подробного описания констант PHP_INI_*, обратитесь к документации функции ini_set().

Краткое разъяснение конфигурационных директив.

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). As of PHP 4.3.5, this directive is always Off in PHP-CLI.

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.

Замечание: You cannot use both mb_output_handler() with ob_iconv_handler() and you cannot use both ob_gzhandler() and zlib.output_compression.

Замечание: Only built-in functions can be used with this directive. For user defined functions, use ob_start().

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().

Типы ресурсов

Данное расширение не определяет никакие типы ресурсов.

Предопределенные константы

Данное расширение не определяет никакие константы.

Примеры

Пример 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.)

Замечание: 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 not be hidden from output.

Содержание
flush -- Flush the output buffer
ob_clean --  Clean (erase) the output buffer
ob_end_clean --  Clean (erase) the output buffer and turn off output buffering
ob_end_flush --  Flush (send) the output buffer and turn off output buffering
ob_flush --  Flush (send) the output buffer
ob_get_clean --  Get current buffer contents and delete current output buffer
ob_get_contents --  Return the contents of the output buffer
ob_get_flush --  Flush the output buffer, return it as a string and turn off output buffering
ob_get_length --  Return the length of the output buffer
ob_get_level --  Return the nesting level of the output buffering mechanism
ob_get_status --  Get status of output buffers
ob_gzhandler --  ob_start callback function to gzip output buffer
ob_implicit_flush --  Turn implicit flush on/off
ob_list_handlers --  List all output handlers in use
ob_start -- Turn on output buffering
output_add_rewrite_var --  Add URL rewriter values
output_reset_rewrite_vars --  Reset URL rewriter values


Output Control Functions
webmaster [at] oltec-solutions [dot] nl
10-Jul-2006 06:00
In re to erwinX at darwineX dot nl:

Adding an ampersand (&) before the hash seems to work too (for me at least), i.e.: http://somedomain.tld/blah.php?arg=x&#something

I guess php then interperts it as an argument to the script. Might save some time and resources.
erwinX at darwineX dot nl
11-Nov-2005 05:35
[Concerns IE refusing to jump to a #something in the URL.]

I encoutered a bug in IE6/W2000 that can be solved by turning output buffering on.
Maybe it also helps in other situations/M$-OS, not sure.

Situation:
A page with a hash in the URL, and IE doesn't jump to that location.

Example:
http://www.bla.com/test.php#something

- In test.php the anchortag is placed normally like:
<a name="something"><br></a>

- test.php takes a few seconds to load because of heavy-duty database activity.

IE just ignores the hash #something.

It looks like IE 'forgets' the hash if it hasn't encoutered it YET in the HTML.

Turning output buffering on resolves that issue.
kend52 at verizon dot net
23-Jun-2005 11:25
I ran out of memory, while output buffering and drawing text on imported images. Only the top portion of the 5MP image was displayed by the browser.  Try increasing the memory limit in either the php.ini file( memory_limit = 16M; ) or in the .htaccess file( php_value memory_limit "16M" ). Also see function memory_get_usage() .
gruik at libertysurf dot fr
10-Jul-2004 05:53
For those who are looking for optimization, try using buffered output.

I noticed that an output function call (i.e echo()) is somehow time expensive. When using buffered output, only one output function call is made and it seems to be much faster.
Try this :

<?php
your_benchmark_start_function
();

for (
$i = 0; $i < 5000; $i++)
   echo
str_repeat ("your string blablabla bla bla", (rand() % 4) + 1)."<br>\n";

echo
your_benchmark_end_function();
?>

And then :

<?php
your_benchmark_start_function
();

ob_start ();
for (
$i = 0; $i < 5000; $i++)
   echo
str_repeat ("your string blablabla bla bla", (rand() % 4) + 1)."<br>\n";

echo
your_benchmark_end_function();
ob_end_flush ();
?>
nobbie @t php d0t net
01-Apr-2004 04:49
There is a problem in MSIE 5.5,6 with regards to Page compression. Users might experience pages not loading completely, or just a blank page.

This articles you are looking for is what you're looking for:
 Microsoft Knowledge Base Article - 312496 (for MSIE 6)
 Microsoft Knowledge Base Article - 313712 (for MSIE 5.5)

It states that you should upgrade to the latest MSIE Service Pack to fix the following problem:

Internet Explorer May Lose the First 2,048 Bytes of Data That Are Sent Back from a Web Server That Uses HTTP Compression
tijmen
09-Jul-2003 04:44
Trying to benchmark your server when using output_buffering ?
Don't forget that the value 4096 in the php.ini will give you complete different loadtimes compares to the value of 1.
In the first case the output will be sent after buffering 4096 and the loadtime timed at the end of the page will contain the loadtime needed to download the complete page in the clientbrowser while the second value will contain the loadtime needed to place the complete page in the buffer. The time needed for sending is not clocked.
This can be very frustrating if you don't see the differance between server and the 1st is using 4096 instead of 1.
Although technically much faster than the second server the second server was providing much better loadtime results.
This result will grow when using large amounts of output.
But this becomes interesting if you want to measure the time needed for the page to be loaded for the client.
philip at thepr()jects dot ()rg
08-Feb-2001 11:17

<PDO_OCI DSNflush>
 Last updated: Tue, 15 Nov 2005