Output Control FunctionsOutput Control
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.
Output Control example
]]>
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.)
See also header and
setcookie.
flushFlush the output bufferDescriptionvoidflush
Flushes the output buffers of PHP and whatever backend PHP is
using (CGI, a web server, etc). This effectively tries to push
all the output so far to the user's browser.
flush has no effect on the buffering
scheme of your webserver or the browser on the client
side.
Several servers, especially on Win32, will still buffer
the output from your script until it terminates before
transmitting the results to the browser.
Server modules for Apache like mod_gzip may do buffering of their own
that will cause flush to not result in data being
sent immediately to the client.
Even the browser may buffer its input before displaying it.
Netscape, for example, buffers text until it receives an
end-of-line or the beginning of a tag, and it won't render
tables until the </table> tag of the outermost table is
seen.
Some versions of Microsoft Internet Explorer will only start to display
the page after they have received 256 bytes of output, so you may need to
send extra whitespace before flushing to get those browsers to display the
page.
ob_startTurn on output bufferingDescriptionvoidob_startstring
output_callback
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.
The contents of this internal buffer may be copied into a string
variable using ob_get_contents. To output
what is stored in the internal buffer, use
ob_end_flush. Alternatively,
ob_end_clean will silently discard the
buffer contents.
An optional output_callback function may
be specified. This function takes a string as a parameter and
should return a string. The function will be called when
ob_end_flush is called, or when the output
buffer is flushed to the browser at the end of the request. When
output_callback 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.
In PHP 4.0.4, ob_gzhandler was introduced
to facilitate sending gz-encoded data to web browsers that
support compressed web pages. ob_gzhandler
determines what type of content encoding the browser will accept
and will return it's output accordingly.
Output buffers are stackable, that is, you may call
ob_start while another
ob_start is active. Just make
sure that you call ob_end_flush
the appropriate number of times. If multiple output callback
functions are active, output is being filtered sequentially
through each of them in nesting order.
ob_end_clean,
ob_end_flush, ob_clean,
ob_flush and ob_start
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.
User defined callback function example