PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/php/function.ob-start.html

https://bitbucket.org/thncr/manuals
HTML | 236 lines | 197 code | 39 blank | 0 comment | 0 complexity | c9ffdb323dfde2e00278203771fe6f03 MD5 | raw file
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  5. <title>打开输出控制缓冲</title>
  6. </head>
  7. <body><div class="manualnavbar" style="text-align: center;">
  8. <div class="prev" style="text-align: left; float: left;"><a href="function.ob-list-handlers.html">ob_list_handlers</a></div>
  9. <div class="next" style="text-align: right; float: right;"><a href="function.output-add-rewrite-var.html">output_add_rewrite_var</a></div>
  10. <div class="up"><a href="ref.outcontrol.html">Output Control 函数</a></div>
  11. <div class="home"><a href="index.html">PHP Manual</a></div>
  12. </div><hr /><div id="function.ob-start" class="refentry">
  13. <div class="refnamediv">
  14. <h1 class="refname">ob_start</h1>
  15. <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">ob_start</span> &mdash; <span class="dc-title">打开输出控制缓冲</span></p>
  16. </div>
  17. <div class="refsect1 description" id="refsect1-function.ob-start-description">
  18. <h3 class="title">说明</h3>
  19. <div class="methodsynopsis dc-description">
  20. <span class="type">bool</span> <span class="methodname"><strong>ob_start</strong></span>
  21. ([ <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.callback" class="type callback">callback</a></span> <code class="parameter">$output_callback</code></span>
  22. [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$chunk_size</code></span>
  23. [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$erase</code></span>
  24. ]]] )</div>
  25. <p class="para rdfs-comment">
  26. 此函数将打开输出缓冲当输出缓冲是活跃的时候没有输出能从脚本送出除http标头外相反输 出的内容被存储在内部缓冲区中
  27. </p>
  28. <p class="para">
  29. 内部缓冲区的内容可以用 <span class="function"><a href="function.ob-get-contents.html" class="function">ob_get_contents()</a></span> 函数复制到一个字符串变量中
  30. 想要输出存储在内部缓冲区中的内容可以使用 <span class="function"><a href="function.ob-end-flush.html" class="function">ob_end_flush()</a></span> 函数另外
  31. 使用 <span class="function"><a href="function.ob-end-clean.html" class="function">ob_end_clean()</a></span> 函数会静默丢弃掉缓冲区的内容
  32. </p>
  33. <div class="warning"><strong class="warning">Warning</strong>
  34. <p class="para">
  35. 当有正在调用的回调函数时一些网络服务器例如Apache会改变一个脚本的工作目录
  36. 你可以通过例如<em>chdir(dirname($_SERVER[&#039;SCRIPT_FILENAME&#039;]))</em>
  37. 在回调函数中再把它改回来
  38. </p>
  39. </div>
  40. <p class="para">
  41. 输出缓冲区是可堆叠的这即意谓着当有一个 <span class="function"><strong>ob_start()</strong></span> 是活跃的时
  42. 你可以调用另一个 <span class="function"><strong>ob_start()</strong></span>
  43. 只要确保又正确调用了 <span class="function"><a href="function.ob-end-flush.html" class="function">ob_end_flush()</a></span> 恰当的次数即可
  44. 如果有多重输出回调函数是活跃的输出内容会一直按嵌套的顺序依次通过它们而被过滤
  45. </p>
  46. </div>
  47. <div class="refsect1 parameters" id="refsect1-function.ob-start-parameters">
  48. <h3 class="title">参数</h3>
  49. <p class="para">
  50. <dl>
  51. <dt>
  52. <span class="term"><em><code class="parameter">output_callback</code></em></span>
  53. <dd>
  54. <p class="para">
  55. 可选参数 <em><code class="parameter">output_callback</code></em> 函数可以被指定
  56. 此函数把一个字符串当作参数并返回一个字符串
  57. 此函数在下情况会被调用到输出缓冲区被( <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> 或者相似的函数)冲刷送出或者被清洗的时候或者在请求结束之际输出缓冲区内容被冲刷到浏览器时
  58. <em><code class="parameter">output_callback</code></em> 被调用时它将收到输出缓冲区的内容作为参数
  59. 并预期返回一个新的输出缓冲区作为结果这个新返回的输出缓冲区内容将被送到浏览器
  60. 如果这个 <em><code class="parameter">output_callback</code></em> 不是一个可以调用的函数此函数
  61. 会返回 <strong><code>FALSE</code></strong>
  62. </p>
  63. <p class="para">
  64. 如果回调函数有两个参数第二个参数会由一个位域补充该位域由
  65. <strong><code>PHP_OUTPUT_HANDLER_START</code></strong>,
  66. <strong><code>PHP_OUTPUT_HANDLER_CONT</code></strong>
  67. <strong><code>PHP_OUTPUT_HANDLER_END</code></strong> 组成
  68. </p>
  69. <p class="para">
  70. 如果 <em><code class="parameter">output_callback</code></em> 返回 <strong><code>FALSE</code></strong> 其原来的输入
  71. 内容被直接送到浏览器
  72. </p>
  73. <p class="para">
  74. 这个参数 <em><code class="parameter">output_callback</code></em> 可以通过直接给一个 <strong><code>NULL</code></strong>
  75. 值而避开
  76. </p>
  77. <p class="para">
  78. <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>,
  79. <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
  80. <span class="function"><strong>ob_start()</strong></span> 不能从一个回调函数中调用
  81. 如果从回调函数中调用了它们产生的行为是不明确的
  82. 如果想要删除缓冲区的内容从回调函数中返回一个&quot;&quot; (空字符串)
  83. 更不能从一个回调函数中使用像<em>print_r($expression, true)</em>
  84. <em>highlight_file($filename, true)</em> 一样的输出缓冲函数
  85. </p>
  86. <blockquote class="note"><p><strong class="note">Note</strong>:
  87. <p class="para">
  88. 在PHP 4.0.4 <span class="function"><a href="function.ob-gzhandler.html" class="function">ob_gzhandler()</a></span> 被引入是为了简化把gz编码过
  89. 数据发送到支持压缩网页的浏览器
  90. <span class="function"><a href="function.ob-gzhandler.html" class="function">ob_gzhandler()</a></span> 会判定浏览器可以接受哪种类型的编码内容并返回相应
  91. 的输出
  92. </p>
  93. </p></blockquote>
  94. </dd>
  95. </dt>
  96. <dt>
  97. <span class="term"><em><code class="parameter">chunk_size</code></em></span>
  98. <dd>
  99. <p class="para">
  100. 如果可选参数 <em><code class="parameter">chunk_size</code></em> 被赋值了在任何一个能引起缓冲区的长度等于
  101. 或超过 <em><code class="parameter">chunk_size</code></em> 的输出操作后缓冲区都会被刷送
  102. 默认值 0 意味着函数仅在最后被调用其余的特殊值可以将 <em><code class="parameter">chunk_size</code></em>
  103. 1 设定到 4096
  104. </p>
  105. </dd>
  106. </dt>
  107. <dt>
  108. <span class="term"><em><code class="parameter">erase</code></em></span>
  109. <dd>
  110. <p class="para">
  111. 如果可选参数 <em><code class="parameter">erase</code></em> 被赋成 <strong><code>FALSE</code></strong>直到脚本执行完成缓冲区才被删除
  112. 这使得如果调用了冲刷和清洗清除函数会抛出一个notice,并返回 <strong><code>FALSE</code></strong>
  113. </p>
  114. </dd>
  115. </dt>
  116. </dl>
  117. </p>
  118. </div>
  119. <div class="refsect1 returnvalues" id="refsect1-function.ob-start-returnvalues">
  120. <h3 class="title">返回值</h3>
  121. <p class="para">
  122. 成功时返回 <strong><code>TRUE</code></strong> 或者在失败时返回 <strong><code>FALSE</code></strong>.
  123. </p>
  124. </div>
  125. <div class="refsect1 changelog" id="refsect1-function.ob-start-changelog">
  126. <h3 class="title">更新日志</h3>
  127. <p class="para">
  128. <table class="doctable informaltable">
  129. <thead>
  130. <tr>
  131. <th>版本</th>
  132. <th>说明</th>
  133. </tr>
  134. </thead>
  135. <tbody class="tbody">
  136. <tr>
  137. <td>4.3.2</td>
  138. <td>
  139. 在传递的 <em><code class="parameter">output_callback</code></em> 不能被执行时此函数
  140. 被改成返回 <strong><code>FALSE</code></strong>
  141. </td>
  142. </tr>
  143. <tr>
  144. <td>4.2.0</td>
  145. <td>
  146. 添加了 <em><code class="parameter">erase</code></em> 参数
  147. </td>
  148. </tr>
  149. </tbody>
  150. </table>
  151. </p>
  152. </div>
  153. <div class="refsect1 examples" id="refsect1-function.ob-start-examples">
  154. <h3 class="title">范例</h3>
  155. <p class="para">
  156. <div class="example" id="example-428">
  157. <p><strong>Example #1 用户自定义回调函数的例子</strong></p>
  158. <div class="example-contents">
  159. <div class="phpcode"><code><span style="color: #000000">
  160. <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>
  161. </span>
  162. </code></div>
  163. </div>
  164. <div class="example-contents"><p>以上例程会输出</p></div>
  165. <div class="example-contents screen">
  166. <div class="cdata"><pre>
  167. &lt;html&gt;
  168. &lt;body&gt;
  169. &lt;p&gt;It&#039;s like comparing oranges to oranges.&lt;/p&gt;
  170. &lt;/body&gt;
  171. &lt;/html&gt;
  172. </pre></div>
  173. </div>
  174. </div>
  175. </p>
  176. </div>
  177. <div class="refsect1 seealso" id="refsect1-function.ob-start-seealso">
  178. <h3 class="title">参见</h3>
  179. <p class="para">
  180. <ul class="simplelist">
  181. <li class="member"> <span class="function"><a href="function.ob-get-contents.html" class="function" rel="rdfs-seeAlso">ob_get_contents()</a> - 返回输出缓冲区的内容</span></li>
  182. <li class="member"> <span class="function"><a href="function.ob-end-clean.html" class="function" rel="rdfs-seeAlso">ob_end_clean()</a> - 清空擦除缓冲区并关闭输出缓冲</span></li>
  183. <li class="member"> <span class="function"><a href="function.ob-end-flush.html" class="function" rel="rdfs-seeAlso">ob_end_flush()</a> - 冲刷出送出输出缓冲区内容并关闭缓冲</span></li>
  184. <li class="member"> <span class="function"><a href="function.ob-implicit-flush.html" class="function" rel="rdfs-seeAlso">ob_implicit_flush()</a> - 打开/关闭绝对刷送</span></li>
  185. <li class="member"> <span class="function"><a href="function.ob-gzhandler.html" class="function" rel="rdfs-seeAlso">ob_gzhandler()</a> - 在ob_start中使用的用来压缩输出缓冲区中内容的回调函数ob_start callback function to gzip output buffer</span></li>
  186. <li class="member"> <span class="function"><a href="function.ob-iconv-handler.html" class="function" rel="rdfs-seeAlso">ob_iconv_handler()</a> - 以输出缓冲处理程序转换字符编码</span></li>
  187. <li class="member"> <span class="function"><a href="function.mb-output-handler.html" class="function" rel="rdfs-seeAlso">mb_output_handler()</a> - 在输出缓冲中转换字符编码的回调函数</span></li>
  188. <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>
  189. </ul>
  190. </p>
  191. </div>
  192. </div><hr /><div class="manualnavbar" style="text-align: center;">
  193. <div class="prev" style="text-align: left; float: left;"><a href="function.ob-list-handlers.html">ob_list_handlers</a></div>
  194. <div class="next" style="text-align: right; float: right;"><a href="function.output-add-rewrite-var.html">output_add_rewrite_var</a></div>
  195. <div class="up"><a href="ref.outcontrol.html">Output Control 函数</a></div>
  196. <div class="home"><a href="index.html">PHP Manual</a></div>
  197. </div></body></html>