<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Turn on output buffering</title> </head> <body><div class="manualnavbar" 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> — <span class="dc-title">Turn on output buffering</span></p> </div> <div class="refsect1 description" id="refsect1-function.ob-start-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>ob_start</strong></span> ([ <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$output_callback</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$chunk_size</code><span class="initializer"> = 0</span></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = <strong><code>PHP_OUTPUT_HANDLER_STDFLAGS</code></strong></span></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 <span class="function"><a href="function.ob-get-contents.html" class="function">ob_get_contents()</a></span>. To output what is stored in the internal buffer, use <span class="function"><a href="function.ob-end-flush.html" class="function">ob_end_flush()</a></span>. Alternatively, <span class="function"><a href="function.ob-end-clean.html" class="function">ob_end_clean()</a></span> will silently discard the buffer contents. </p> <div class="warning"><strong class="warning">Warning</strong> <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. <em>chdir(dirname($_SERVER['SCRIPT_FILENAME']))</em> in the callback function. </p> </div> <p class="para"> Output buffers are stackable, that is, you may call <span class="function"><strong>ob_start()</strong></span> while another <span class="function"><strong>ob_start()</strong></span> is active. Just make sure that you call <span class="function"><a href="function.ob-end-flush.html" class="function">ob_end_flush()</a></span> 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" id="refsect1-function.ob-start-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">output_callback</code></em></span> <dd> <p class="para"> An optional <em><code class="parameter">output_callback</code></em> function may be specified. This function takes a string as a parameter and should return a string. The function will be called when the output buffer is flushed (sent) or cleaned (with <span class="function"><a href="function.ob-flush.html" class="function">ob_flush()</a></span>, <span class="function"><a href="function.ob-clean.html" class="function">ob_clean()</a></span> or similar function) or when the output buffer is flushed to the browser at the end of the request. When <em><code class="parameter">output_callback</code></em> 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 <em><code class="parameter">output_callback</code></em> is not a callable function, this function will return <strong><code>FALSE</code></strong>. This is the callback signature: </p> <p class="para"> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><span class="replaceable">handler</span></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$buffer</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$phase</code></span> ] )</div> <dl> <dt> <span class="term"><em><code class="parameter">buffer</code></em></span> <dd> <span class="simpara"> Contents of the output buffer. </span> </dd> </dt> <dt> <span class="term"><em><code class="parameter">phase</code></em></span> <dd> <span class="simpara"> Bitmask of <strong><code>PHP_OUTPUT_HANDLER_*</code></strong> constants. </span> </dd> </dt> </dl> </p> <p class="para"> If <em><code class="parameter">output_callback</code></em> returns <strong><code>FALSE</code></strong> original input is sent to the browser. </p> <p class="para"> The <em><code class="parameter">output_callback</code></em> parameter may be bypassed by passing a <strong><code>NULL</code></strong> value. </p> <p class="para"> <span class="function"><a href="function.ob-end-clean.html" class="function">ob_end_clean()</a></span>, <span class="function"><a href="function.ob-end-flush.html" class="function">ob_end_flush()</a></span>, <span class="function"><a href="function.ob-clean.html" class="function">ob_clean()</a></span>, <span class="function"><a href="function.ob-flush.html" class="function">ob_flush()</a></span> and <span class="function"><strong>ob_start()</strong></span> 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 <em>print_r($expression, true)</em> or <em>highlight_file($filename, true)</em> from a callback function. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> In PHP 4.0.4, <span class="function"><a href="function.ob-gzhandler.html" class="function">ob_gzhandler()</a></span> was introduced to facilitate sending gz-encoded data to web browsers that support compressed web pages. <span class="function"><a href="function.ob-gzhandler.html" class="function">ob_gzhandler()</a></span> determines what type of content encoding the browser will accept and will return its output accordingly. </p> </p></blockquote> </dd> </dt> <dt> <span class="term"><em><code class="parameter">chunk_size</code></em></span> <dd> <p class="para"> If the optional parameter <em><code class="parameter">chunk_size</code></em> is passed, the buffer will be flushed after any output call which causes the buffer's length to equal or exceed <em><code class="parameter">chunk_size</code></em>. The default value <em>0</em> means that the output function will only be called when the output buffer is closed. </p> <p class="para"> Prior to PHP 5.4.0, the value <em>1</em> was a special case value that set the chunk size to 4096 bytes. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">flags</code></em></span> <dd> <p class="para"> The <em><code class="parameter">flags</code></em> parameter is a bitmask that controls the operations that can be performed on the output buffer. The default is to allow output buffers to be cleaned, flushed and removed, which can be set explicitly via <strong><code>PHP_OUTPUT_HANDLER_CLEANABLE</code></strong> | <strong><code>PHP_OUTPUT_HANDLER_FLUSHABLE</code></strong> | <strong><code>PHP_OUTPUT_HANDLER_REMOVABLE</code></strong>, or <strong><code>PHP_OUTPUT_HANDLER_STDFLAGS</code></strong> as shorthand. </p> <p class="para"> Each flag controls access to a set of functions, as described below: <table class="doctable informaltable"> <thead> <tr> <th>Constant</th> <th>Functions</th> </tr> </thead> <tbody class="tbody"> <tr> <td><strong><code>PHP_OUTPUT_HANDLER_CLEANABLE</code></strong></td> <td> <span class="function"><a href="function.ob-clean.html" class="function">ob_clean()</a></span>, <span class="function"><a href="function.ob-end-clean.html" class="function">ob_end_clean()</a></span>, and <span class="function"><a href="function.ob-get-clean.html" class="function">ob_get_clean()</a></span>. </td> </tr> <tr> <td><strong><code>PHP_OUTPUT_HANDLER_FLUSHABLE</code></strong></td> <td> <span class="function"><a href="function.ob-end-flush.html" class="function">ob_end_flush()</a></span>, <span class="function"><a href="function.ob-flush.html" class="function">ob_flush()</a></span>, and <span class="function"><a href="function.ob-get-flush.html" class="function">ob_get_flush()</a></span>. </td> </tr> <tr> <td><strong><code>PHP_OUTPUT_HANDLER_REMOVABLE</code></strong></td> <td> <span class="function"><a href="function.ob-end-clean.html" class="function">ob_end_clean()</a></span>, <span class="function"><a href="function.ob-end-flush.html" class="function">ob_end_flush()</a></span>, and <span class="function"><a href="function.ob-get-flush.html" class="function">ob_get_flush()</a></span>. </td> </tr> </tbody> </table> </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.ob-start-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 changelog" id="refsect1-function.ob-start-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.4.0</td> <td> The third parameter of <span class="function"><strong>ob_start()</strong></span> changed from a <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> parameter called <em><code class="parameter">erase</code></em> (which, if set to <strong><code>FALSE</code></strong>, would prevent the output buffer from being deleted until the script finished executing) to an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> parameter called <em><code class="parameter">flags</code></em>. Unfortunately, this results in an API compatibility break for code written prior to PHP 5.4.0 that uses the third parameter. See <a href="function.ob-start.html#function.ob-start.flags-bc" class="link">the flags example</a> for an example of how to handle this with code that needs to be compatible with both. </td> </tr> <tr> <td>5.4.0</td> <td> A chunk size of <em>1</em> now results in chunks of 1 byte being sent to the output buffer. </td> </tr> <tr> <td>4.3.2</td> <td> This function was changed to return <strong><code>FALSE</code></strong> in case the passed <em><code class="parameter">output_callback</code></em> can not be executed. </td> </tr> <tr> <td>4.2.0</td> <td> Added the <em><code class="parameter">erase</code></em> parameter. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.ob-start-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-473"> <p><strong>Example #1 User defined callback function example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">callback</span><span style="color: #007700">(</span><span style="color: #0000BB">$buffer</span><span style="color: #007700">)<br />{<br /> </span><span style="color: #FF8000">// replace all the apples with oranges<br /> </span><span style="color: #007700">return (</span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"apples"</span><span style="color: #007700">, </span><span style="color: #DD0000">"oranges"</span><span style="color: #007700">, </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">?><br /></span><html><br /><body><br /><p>It's like comparing apples to oranges.</p><br /></body><br /></html><br /><span style="color: #0000BB"><?php<br /><br />ob_end_flush</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> <html> <body> <p>It's like comparing oranges to oranges.</p> </body> </html> </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="function.ob-start.flags-bc"> <p><strong>Example #2 Creating an uneraseable output buffer in a way compatible with both PHP 5.3 and 5.4</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">version_compare</span><span style="color: #007700">(</span><span style="color: #0000BB">PHP_VERSION</span><span style="color: #007700">, </span><span style="color: #DD0000">'5.4.0'</span><span style="color: #007700">, </span><span style="color: #DD0000">'>='</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">ob_start</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_OUTPUT_HANDLER_STDFLAGS </span><span style="color: #007700">^<br /> </span><span style="color: #0000BB">PHP_OUTPUT_HANDLER_REMOVABLE</span><span style="color: #007700">);<br />} else {<br /> </span><span style="color: #0000BB">ob_start</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.ob-start-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.ob-get-contents.html" class="function" rel="rdfs-seeAlso">ob_get_contents()</a> - Return the contents of the output buffer</span></li> <li class="member"> <span class="function"><a href="function.ob-end-clean.html" class="function" rel="rdfs-seeAlso">ob_end_clean()</a> - Clean (erase) the output buffer and turn off output buffering</span></li> <li class="member"> <span class="function"><a href="function.ob-end-flush.html" class="function" rel="rdfs-seeAlso">ob_end_flush()</a> - Flush (send) the output buffer and turn off output buffering</span></li> <li class="member"> <span class="function"><a href="function.ob-implicit-flush.html" class="function" rel="rdfs-seeAlso">ob_implicit_flush()</a> - Turn implicit flush on/off</span></li> <li class="member"> <span class="function"><a href="function.ob-gzhandler.html" class="function" rel="rdfs-seeAlso">ob_gzhandler()</a> - ob_start callback function to gzip output buffer</span></li> <li class="member"> <span class="function"><a href="function.ob-iconv-handler.html" class="function" rel="rdfs-seeAlso">ob_iconv_handler()</a> - Convert character encoding as output buffer handler</span></li> <li class="member"> <span class="function"><a href="function.mb-output-handler.html" class="function" rel="rdfs-seeAlso">mb_output_handler()</a> - Callback function converts character encoding in output buffer</span></li> <li class="member"> <span class="function"><a href="function.ob-tidyhandler.html" class="function" rel="rdfs-seeAlso">ob_tidyhandler()</a> - ob_start callback function to repair the buffer</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" 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>