function.ob-start.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 262 行

HTML
262
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Turn on output buffering</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.ob-list-handlers.html">ob_list_handlers</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.output-add-rewrite-var.html">output_add_rewrite_var</a></div> <div class="up"><a href="ref.outcontrol.html">Output Control Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.ob-start" class="refentry"> <div class="refnamediv">  <h1 class="refname">ob_start</h1>  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">ob_start</span> &mdash; <span class="dc-title">Turn on output buffering</span></p> </div>  <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type">bool</span> <span class="methodname"><b><b>ob_start</b></b></span>    ([ <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.callback" class="type callback">callback</a></span> <tt class="parameter">$output_callback</tt></span>   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$chunk_size</tt></span>   [, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$erase</tt></span>  ]]] )</div>  <p class="para rdfs-comment">   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 class="para">   The contents of this internal buffer may be copied into a string variable   using <a href="function.ob-get-contents.html" class="function">ob_get_contents()</a>.  To output what is stored in   the internal buffer, use <a href="function.ob-end-flush.html" class="function">ob_end_flush()</a>. Alternatively,   <a href="function.ob-end-clean.html" class="function">ob_end_clean()</a> will silently discard the buffer   contents.  </p>  <div class="warning"><b class="warning">Warning</b>   <p class="para">    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.    <i>chdir(dirname($_SERVER[&#039;SCRIPT_FILENAME&#039;]))</i> in the    callback function.   </p>  </div>  <p class="para">   Output buffers are stackable, that is, you may call   <b>ob_start()</b> while another   <b>ob_start()</b> is active. Just make   sure that you call <a href="function.ob-end-flush.html" class="function">ob_end_flush()</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> </div> <div class="refsect1 parameters">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">output_callback</tt></i></span>     <dd>      <p class="para">       An optional <i><tt class="parameter">output_callback</tt></i> 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" class="function">ob_end_flush()</a> is called, or when the output buffer       is flushed to the browser at the end of the request.  When       <i><tt class="parameter">output_callback</tt></i> 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 <i><tt class="parameter">output_callback</tt></i> is not a       callable function, this function will return <b><tt>FALSE</tt></b>.      </p>      <p class="para">       If the callback function has two parameters, the second parameter is       filled with a bit-field consisting of       <b><tt>PHP_OUTPUT_HANDLER_START</tt></b>,       <b><tt>PHP_OUTPUT_HANDLER_CONT</tt></b> and       <b><tt>PHP_OUTPUT_HANDLER_END</tt></b>.      </p>      <p class="para">       If <i><tt class="parameter">output_callback</tt></i> returns <b><tt>FALSE</tt></b> original       input is sent to the browser.      </p>      <p class="para">       The <i><tt class="parameter">output_callback</tt></i> parameter may be bypassed       by passing a <b><tt>NULL</tt></b> value.      </p>      <p class="para">       <a href="function.ob-end-clean.html" class="function">ob_end_clean()</a>, <a href="function.ob-end-flush.html" class="function">ob_end_flush()</a>,       <a href="function.ob-clean.html" class="function">ob_clean()</a>, <a href="function.ob-flush.html" class="function">ob_flush()</a> and       <b>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 &quot;&quot; (a null string) from callback function.       You can&#039;t even call functions using the output buffering functions like       <i>print_r($expression, true)</i> or       <i>highlight_file($filename, true)</i> from a callback       function.      </p>      <blockquote><p><b class="note">Note</b>:                In PHP 4.0.4, <a href="function.ob-gzhandler.html" class="function">ob_gzhandler()</a> was introduced to        facilitate sending gz-encoded data to web browsers that support        compressed web pages.  <a href="function.ob-gzhandler.html" class="function">ob_gzhandler()</a> determines        what type of content encoding the browser will accept and will return        its output accordingly.       <br />      </p></blockquote>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">chunk_size</tt></i></span>     <dd>      <p class="para">       If the optional parameter <i><tt class="parameter">chunk_size</tt></i> is passed, the       buffer will be flushed after any output call which causes the buffer&#039;s       length to equal or exceed <i><tt class="parameter">chunk_size</tt></i>.       Default value 0 means that the function is called only in the end,       other special value 1 sets <i><tt class="parameter">chunk_size</tt></i> to 4096.      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">erase</tt></i></span>     <dd>      <p class="para">       If the optional parameter <i><tt class="parameter">erase</tt></i> is set to <b><tt>FALSE</tt></b>,       the buffer will not be deleted until the script finishes (as of PHP 4.3.0).      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   Returns <b><tt>TRUE</tt></b> on success or <b><tt>FALSE</tt></b> on failure.  </p> </div> <div class="refsect1 changelog">  <h3 class="title">ChangeLog</h3>  <p class="para">   <table class="informaltable">    <colgroup>     <thead valign="middle">      <tr valign="middle">       <th colspan="1">Version</th>       <th colspan="1">Description</th>      </tr>     </thead>     <tbody valign="middle" class="tbody">      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">4.3.2</td>       <td colspan="1" rowspan="1" align="left">        This function was changed to return <b><tt>FALSE</tt></b> in case the passed        <i><tt class="parameter">output_callback</tt></i> can not be executed.       </td>      </tr>     </tbody>    </colgroup>   </table>  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 User defined callback function example</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">callback</span><span style="color: #007700">(</span><span style="color: #0000BB">$buffer</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;replace&nbsp;all&nbsp;the&nbsp;apples&nbsp;with&nbsp;oranges<br />&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;(</span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"apples"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"oranges"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$buffer</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #0000BB">ob_start</span><span style="color: #007700">(</span><span style="color: #DD0000">"callback"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;<br /></span>&lt;html&gt;<br />&lt;body&gt;<br />&lt;p&gt;It's&nbsp;like&nbsp;comparing&nbsp;apples&nbsp;to&nbsp;oranges.&lt;/p&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /><span style="color: #0000BB">&lt;?php<br /><br />ob_end_flush</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p>The above example will output:</p></div>    <div class="example-contents"><pre><div class="cdata"><pre>&lt;html&gt;&lt;body&gt;&lt;p&gt;It&#039;s like comparing oranges to oranges.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre></div>    </pre></div>   </div>  </p> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.ob-get-contents.html" class="function" rel="rdfs-seeAlso">ob_get_contents()</a></li>    <li class="member"><a href="function.ob-end-clean.html" class="function" rel="rdfs-seeAlso">ob_end_clean()</a></li>    <li class="member"><a href="function.ob-end-flush.html" class="function" rel="rdfs-seeAlso">ob_end_flush()</a></li>    <li class="member"><a href="function.ob-implicit-flush.html" class="function" rel="rdfs-seeAlso">ob_implicit_flush()</a></li>    <li class="member"><a href="function.ob-gzhandler.html" class="function" rel="rdfs-seeAlso">ob_gzhandler()</a></li>    <li class="member"><a href="function.ob-iconv-handler.html" class="function" rel="rdfs-seeAlso">ob_iconv_handler()</a></li>    <li class="member"><a href="function.mb-output-handler.html" class="function" rel="rdfs-seeAlso">mb_output_handler()</a></li>    <li class="member"><a href="function.ob-tidyhandler.html" class="function" rel="rdfs-seeAlso">ob_tidyhandler()</a></li>   </ul>  </p> </div>  </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.ob-list-handlers.html">ob_list_handlers</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.output-add-rewrite-var.html">output_add_rewrite_var</a></div> <div class="up"><a href="ref.outcontrol.html">Output Control Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?