Home > Article > Backend Development > How to close ob_start in php
php ob_start is used to open the output control buffer. If you turn off ob_start, you can use the ob_end_clean() function to silently discard the contents of the buffer.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to close ob_start in php?
ob_start — Open the output control buffer
Description
ob_start(callable $output_callback = null, int $chunk_size = 0, int $flags = PHP_OUTPUT_HANDLER_STDFLAGS): bool
This function will open the output buffer. When output buffering is activated, the script will not output content (except http headers), instead the content to be output is stored in an internal buffer.
The contents of the internal buffer can be copied to a string variable using the ob_get_contents()
function. To output the content stored in the internal buffer, you can use the ob_end_flush()
function. In addition, using the ob_end_clean()
function will silently discard the contents of the buffer.
WARNING
Some web servers (such as Apache) will change a script's working directory when there is a callback function being called. You can change it back in the callback function, for example chdir(dirname($_SERVER['SCRIPT_FILENAME'])) .
The output buffer is stackable, which means that when one ob_start() is active, you can call another ob_start(). Just make sure you call ob_end_flush() the correct number of times. If multiple output callbacks are active, the output will always be filtered through them in nested order.
Parameters
output_callback
Optional parameters The output_callback function can be specified. This function takes a string as argument and returns a string. This function will be called when the output buffer is flushed (emitted) or cleaned (ob_flush(), ob_clean() or similar functions); or when the output buffer contents are flushed to the browser at the end of the request. transfer. When output_callback is called, it receives the contents of the output buffer as a parameter and is expected to return a new output buffer as a result. The contents of this newly returned output buffer will be sent to the browser. If this output_callback is not a callable function, this function will return false.
The following is the callback signature:
handler(string $buffer, int $phase = ?): string
buffer
The contents of the output buffer.
phase
Bit mask PHP_OUTPUT_HANDLER_* constants.
If output_callback returns false, the original input content is sent directly to the browser.
This parameter output_callback can be avoided by directly giving a null value.
ob_end_clean(), ob_end_flush(), ob_clean(), ob_flush() and ob_start() cannot be called from a callback function. If they are called from a callback function, the resulting behavior is unspecified. If you want to delete the contents of the buffer, return a "" (empty string) from the callback function. It is also not possible to use output buffering functions like print_r($expression, true) or highlight_file($filename, true) from a callback function.
注意: ob_gzhandler() function exists 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 its output accordingly.
chunk_size
If the optional parameter chunk_size is assigned, the buffer will be flushed after any output operation that causes the length of the buffer to be equal to or exceed chunk_size. The default value 0 means the function is called only at the end.
Prior to PHP 5.4.0, 1 was a special case value that set chunk_size to 4096 bytes.
flags
The flags parameter represents a mask bit used to control operations on the buffer. The default is to allow output buffers to be cleaned, flushed and removed, which can be set explicitly via PHP_OUTPUT_HANDLER_CLEANABLE | PHP_OUTPUT_HANDLER_FLUSHABLE | PHP_OUTPUT_HANDLER_REMOVABLE, or PHP_OUTPUT_HANDLER_STDFLAGS as shorthand. , or return false on failure.
Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of How to close ob_start in php. For more information, please follow other related articles on the PHP Chinese website!