<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >ob_start</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Output Control 输出控制函数" HREF="ref.outcontrol.html"><LINK REL="PREVIOUS" TITLE="ob_list_handlers" HREF="function.ob-list-handlers.html"><LINK REL="NEXT" TITLE="output_add_rewrite_var" HREF="function.output-add-rewrite-var.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.ob-list-handlers.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.output-add-rewrite-var.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.ob-start" ></A >ob_start</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN165880" ></A ><P > (PHP 4, PHP 5)</P >ob_start -- Turn on output buffering</DIV ><DIV CLASS="refsect1" ><A NAME="AEN165883" ></A ><H2 >Description</H2 >bool <B CLASS="methodname" >ob_start</B > ( [callback output_callback [, int chunk_size [, bool erase]]] )<BR ></BR ><P > This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. </P ><P > The contents of this internal buffer may be copied into a string variable using <A HREF="function.ob-get-contents.html" ><B CLASS="function" >ob_get_contents()</B ></A >. To output what is stored in the internal buffer, use <A HREF="function.ob-end-flush.html" ><B CLASS="function" >ob_end_flush()</B ></A >. Alternatively, <A HREF="function.ob-end-clean.html" ><B CLASS="function" >ob_end_clean()</B ></A > will silently discard the buffer contents. </P ><P > An optional <CODE CLASS="parameter" >output_callback</CODE > function may be specified. This function takes a string as a parameter and should return a string. The function will be called when <A HREF="function.ob-end-flush.html" ><B CLASS="function" >ob_end_flush()</B ></A > is called, or when the output buffer is flushed to the browser at the end of the request. When <CODE CLASS="parameter" >output_callback</CODE > is called, it will receive the contents of the output buffer as its parameter and is expected to return a new output buffer as a result, which will be sent to the browser. If the <CODE CLASS="parameter" >output_callback</CODE > is not a callable function, this function will return <TT CLASS="constant" ><B >FALSE</B ></TT >. If the callback function has two parameters, the second parameter is filled with a bit-field consisting of <TT CLASS="constant" ><B >PHP_OUTPUT_HANDLER_START</B ></TT >, <TT CLASS="constant" ><B >PHP_OUTPUT_HANDLER_CONT</B ></TT > and <TT CLASS="constant" ><B >PHP_OUTPUT_HANDLER_END</B ></TT >. If <CODE CLASS="parameter" >output_callback</CODE > returns <TT CLASS="constant" ><B >FALSE</B ></TT > original input is sent to the browser. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > In PHP 4.0.4, <A HREF="function.ob-gzhandler.html" ><B CLASS="function" >ob_gzhandler()</B ></A > was introduced to facilitate sending gz-encoded data to web browsers that support compressed web pages. <A HREF="function.ob-gzhandler.html" ><B CLASS="function" >ob_gzhandler()</B ></A > determines what type of content encoding the browser will accept and will return its output accordingly. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > Before PHP 4.3.2 this function did not return <TT CLASS="constant" ><B >FALSE</B ></TT > in case the passed <CODE CLASS="parameter" >output_callback</CODE > can not be executed. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >警告</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > Some web servers (e.g. Apache) change the working directory of a script when calling the callback function. You can change it back by e.g. <TT CLASS="literal" >chdir(dirname($_SERVER['SCRIPT_FILENAME']))</TT > in the callback function. </P ></TD ></TR ></TABLE ></DIV ><P > If the optional parameter <CODE CLASS="parameter" >chunk_size</CODE > is passed, the callback function is called on every first newline after <CODE CLASS="parameter" >chunk_size</CODE > bytes of output. The <CODE CLASS="parameter" >output_callback</CODE > parameter may be bypassed by passing a <TT CLASS="constant" ><B >NULL</B ></TT > value. </P ><P > If the optional parameter <CODE CLASS="parameter" >erase</CODE > is set to <TT CLASS="constant" ><B >FALSE</B ></TT >, the buffer will not be deleted until the script finishes (as of PHP 4.3.0). </P ><P > Output buffers are stackable, that is, you may call <B CLASS="function" >ob_start()</B > while another <B CLASS="function" >ob_start()</B > is active. Just make sure that you call <A HREF="function.ob-end-flush.html" ><B CLASS="function" >ob_end_flush()</B ></A > the appropriate number of times. If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order. </P ><P > <A HREF="function.ob-end-clean.html" ><B CLASS="function" >ob_end_clean()</B ></A >, <A HREF="function.ob-end-flush.html" ><B CLASS="function" >ob_end_flush()</B ></A >, <A HREF="function.ob-clean.html" ><B CLASS="function" >ob_clean()</B ></A >, <A HREF="function.ob-flush.html" ><B CLASS="function" >ob_flush()</B ></A > and <B CLASS="function" >ob_start()</B > may not be called from a callback function. If you call them from callback function, the behavior is undefined. If you would like to delete the contents of a buffer, return "" (a null string) from callback function. You can't even call functions using the output buffering functions like <TT CLASS="literal" >print_r($expression, true)</TT > or <TT CLASS="literal" >highlight_file($filename, true)</TT > from a callback function. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN165945" ></A ><P ><B >例 1. User defined callback function example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /><br /></font><font color="#007700">function </font><font color="#0000BB">callback</font><font color="#007700">(</font><font color="#0000BB">$buffer</font><font color="#007700">) <br />{<br /> </font><font color="#FF8000">// replace all the apples with oranges<br /> </font><font color="#007700">return (</font><font color="#0000BB">str_replace</font><font color="#007700">(</font><font color="#DD0000">"apples"</font><font color="#007700">, </font><font color="#DD0000">"oranges"</font><font color="#007700">, </font><font color="#0000BB">$buffer</font><font color="#007700">));<br />}<br /><br /></font><font color="#0000BB">ob_start</font><font color="#007700">(</font><font color="#DD0000">"callback"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?><br /></font><html><br /><body><br /><p>It's like comparing apples to oranges.</p><br /></body><br /></html><br /><font color="#0000BB"><?php<br /><br />ob_end_flush</font><font color="#007700">();<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > Would produce: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" ><html> <body> <p>It's like comparing oranges to oranges.</p> </body> </html></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also <A HREF="function.ob-get-contents.html" ><B CLASS="function" >ob_get_contents()</B ></A >, <A HREF="function.ob-end-flush.html" ><B CLASS="function" >ob_end_flush()</B ></A >, <A HREF="function.ob-end-clean.html" ><B CLASS="function" >ob_end_clean()</B ></A >, <A HREF="function.ob-implicit-flush.html" ><B CLASS="function" >ob_implicit_flush()</B ></A >, <A HREF="function.ob-gzhandler.html" ><B CLASS="function" >ob_gzhandler()</B ></A >, <A HREF="function.ob-iconv-handler.html" ><B CLASS="function" >ob_iconv_handler()</B ></A > <A HREF="function.mb-output-handler.html" ><B CLASS="function" >mb_output_handler()</B ></A >, and <A HREF="function.ob-tidyhandler.html" ><B CLASS="function" >ob_tidyhandler()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.ob-list-handlers.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.output-add-rewrite-var.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >ob_list_handlers</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.outcontrol.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >output_add_rewrite_var</TD ></TR ></TABLE ></DIV ></BODY ></HTML >