PHP 手冊
前一頁 |  | 下一頁

# XCI. Output Control Functions

# 介紹

The Output Control functions allow you to control when output is sent from the script. This can be useful in several different situations, especially if you need to send headers to the browser after your script has began outputting data. The Output Control functions do not affect headers sent using header() or setcookie(), only functions such as echo() and data between blocks of PHP code.

# 需求

建立這個延伸不需要任何外部程式庫。

# 安裝

這些函數是PHP核心的一部分，不需要安裝就可使用。

# 執行時期組態

`php.ini`中的設定會影響這些函數的行為。

表格 1. Output Control configuration options

Name | Default | Changeable
--- | --- | ---
output_buffering | "0" | PHP_INI_PERDIR|PHP_INI_SYSTEM
output_handler | NULL | PHP_INI_PERDIR|PHP_INI_SYSTEM
implicit_flush | "0" | PHP_INI_PERDIR|PHP_INI_SYSTEM

For further details and definitions of the PHP_INI_* constants, see the 附錄 H.

這裡為簡短的結構指令說明。

output_buffering boolean/integer

  You can enable output buffering for all files by setting this directive to 'On'. If you wish to limit the size of the buffer to a certain size - you can use a maximum number of bytes instead of 'On', as a value for this directive (e.g., output_buffering=4096). As of PHP 4.3.5, this directive is always Off in PHP-CLI.

output_handler string

  You can redirect all of the output of your scripts to a function. For example, if you set output_handler to mb_output_handler(), character encoding will be transparently converted to the specified encoding. Setting any output handler automatically turns on output buffering.

  > 注: You cannot use both mb_output_handler() with ob_iconv_handler() and you cannot use both ob_gzhandler() and zlib.output_compression.

implicit_flush boolean

  `FALSE` by default. Changing this to `TRUE` tells PHP to tell the output layer to flush itself automatically after every output block. This is equivalent to calling the PHP function flush() after each and every call to print() or echo() and each and every HTML block.

  When using PHP within an web environment, turning this option on has serious performance implications and is generally recommended for debugging purposes only. This value defaults to `TRUE` when operating under the CLI SAPI.

  See also ob_implicit_flush().

# 資源類型

這個延伸沒有定義任何資源。

# 預先定義常數

這個延伸沒有定義任何常數。

# 範例

範例 1. Output Control example

`<?php ob_start(); echo "Hello\n"; setcookie("cookiename", "cookiedata"); ob_end_flush(); ?>`

In the above example, the output from echo() would be stored in the output buffer until ob_end_flush() was called. In the mean time, the call to setcookie() successfully stored a cookie without causing an error. (You can not normally send headers to the browser after data has already been sent.)

> 注: When upgrading from PHP 4.1 (and 4.2) to 4.3 that due to a bug in earlier versions you must ensure that implict_flush is OFF in your `php.ini`, otherwise any output with ob_start() will not be hidden from output.

# 參考

See also header() and setcookie().

前一頁 | 內容 | 下一頁
ora_rollback | 上一層 | flush