CI. Output Control 输出控制函数

简介

输出控制函数可以用来控制脚本的输出。这些函数在某些特殊情况下很有用,特别是脚本中已经输出了信息之后再想向浏览器发送标头的情况。输出控制函数不会作用于 header()setcookie() 函数发送的标头,而只会影响类似于 echo() 函数输出的信息和嵌入在 PHP 代码之间的信息。

需求

要编译本扩展模块不需要外部库文件。

安装

本函数库作为 PHP 内核的一部分,不用安装就能使用。

运行时配置

这些函数的行为受 php.ini 的影响。

表格 1. 输出控制配置选项

名称默认值作用范围修正记录
output_buffering"0"PHP_INI_PERDIR 
output_handlerNULLPHP_INI_PERDIR自 PHP 4.0.4 起可用
implicit_flush"0"PHP_INI_ALL在 PHP <= 4.2.3 版本中是 PHP_INI_PERDIR
有关 PHP_INI_* 常量进一步的细节与定义参见附录 G

以下是配置选项的简要解释。

output_buffering boolean/integer

该选项设置为 On 时,将在所有的脚本中使用输出控制。如果要限制输出缓冲区的最大值,可将该选项设定为指定的最大字节数(例如 output_buffering=4096)。从PHP 4.3.5 版开始,该选项在 PHP-CLI 下总是为 Off。

output_handler string

该选项可将脚本所有的输出,重定向到一个函数。例如,将 output_handler 设置为 mb_output_handler() 时,字符的编码将被修改为指定的编码。设置的任何处理函数,将自动的处理输出缓冲。

注: 不能同时使用 mb_output_handler()ob_iconv_handler(),也不能同时使用 ob_gzhandler()zlib.output_compression

注: 只有内置函数可以使用此指令。对于用户定义的函数,使用 ob_start()

implicit_flush boolean

默认为 FALSE。如将该选项改为 TRUE,PHP 将使输出层,在每段信息块输出后,自动刷新。这等同于在每次使用 print()echo() 等函数或每个 HTML 块之后,调用 PHP 中的 flush() 函数。

不在web环境中使用 PHP 时,打开这个选项对程序执行的性能有严重的影响,通常只推荐在调试时使用。在 CLI SAPI 的执行模式下,该标记默认为 TRUE

参见 ob_implicit_flush()

资源类型

本扩展模块未定义任何资源类型。

预定义常量

本扩展模块未定义任何常量。

范例

例子 1. 输出控制例子

<?php

ob_start
();
echo
"Hello\n";

setcookie("cookiename", "cookiedata");

ob_end_flush();

?>

在上面的例子中,函数 echo() 输出信息将被保存在输出缓冲中,直到 ob_end_flush() 函数被调用。和期望的一样,setcookie() 函数存储 cookie 的时候没有导致错误(通常情况下,已经有数据输出后,是不能正常地向浏览器发送标头的)。

注: 当从 PHP 4.1(包括 4.2)升级到 4.3 时,由于早期版本的 bug,必须确保在 php.ini 中将implict_flush 设置成 OFF,否则所有由 ob_start() 函数处理的信息都会被直接输出。

参见

参见 header()setcookie()

目录
flush -- 刷新输出缓冲
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

add a note add a note User Contributed Notes
trucex [um, at] gmail [um, dot] com
16-Sep-2006 05:08
Unfortunately, the PHP guys didn't build support into any of the image output functions to return the image instead of outputting it.

Fortunately, we have output buffering to fix that.

<?

$im
= imagecreatetruecolor(200, 200);

// Other image functions here...

ob_start();
imagepng($im);
$imageData = ob_get_contents();
ob_clean();

?>

You can now use the $imageData variable to either create another GD image, save it, put it in a database, make modifications to the binary, or output it to the user. You can easily check the size of it as well without having to access the disk...just use strlen();
webmaster at wistex dot com
08-Sep-2006 07:07
Now this just blew my mind. I had a problem with MySQL being incredibly slow on Windows 2003 running IIS... on ASP/VBScript pages. PHP is also installed on the server and so is Microsoft SQL 2005 Express. (Yes, we're running ASP, PHP, MySQL and MS SQL on the same Windows 2003 Server using IIS.)

I was browsing the internet for a solution and saw a suggestion that I change output_buffering to on if MySQL was slow for PHP pages.  Since we also served PHP pages with MySQL from the same server, it caught my eye.  For the hell of it, I went into php.ini and changed output_buffering to on and suddenly MySQL and ASP was faster... MySQL and PHP was faster... Microsoft SQL Server 2005 Express and ASP was faster.... everything was faster... even stuff that had no PHP!

And I didn't even have to restart IIS. As soon as I saved the php.ini file with the change, everything got faster.

Apparently PHP and MySQL and IIS are so intertwined somehow that changing the buffering setting really effects the performance of the entire server.

So, if you are having performance problems on Windows 2003 & IIS, you might try setting output_buffering = On in php.ini if you happen to have PHP installed.  Having it set to off apparently effects the performance of Windows 2003 and IIS severely... even for webpages that do not use PHP or MySQL.
kamermans at teratechnologies dot net
22-Aug-2006 04:30
Output buffering is set to '4096' instead of 'Off' or '0' by default in the php-5.0.4-10.5 RPM for Fedora Core release 4 (Stentz).  This has cost me much time!
webmaster [at] oltec-solutions [dot] nl
11-Jul-2006 09: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 09: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
24-Jun-2005 02: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 08: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 08: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 07: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
09-Feb-2001 03:17