Home  >  Article  >  Backend Development  >  PHP functions ob_start(), ob_end_clean(), ob_get_contents()_PHP tutorial

PHP functions ob_start(), ob_end_clean(), ob_get_contents()_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:14:04811browse

The article introduces the PHP functions ob_start(), ob_end_clean(), and ob_get_contents(). Friends in need can refer to them.

Usage of the following three functions

ob_get_contents() - Returns the contents of the output buffer


ob_get_contents

(PHP 4, PHP 5)

ob_get_contents — Returns the contents of the output buffer
Description
string ob_get_contents (void)

Just get the contents of the output buffer, but don't clear it.
Return value

This function returns the contents of the output buffer, or FALSE if the output buffer is invalid.
Example

The code is as follows Copy code
 代码如下 复制代码

Example #1 A simple ob_get_contents() example

ob_start();

echo "Hello ";

$out1 = ob_get_contents();

echo "World";

$out2 = ob_get_contents();

ob_end_clean();

var_dump($out1, $out2);
?>

以上例程会输出:

string(6) "Hello "
string(11) "Hello World"

 

Example #1 A simple ob_get_contents() example

ob_start();

echo "Hello ";

$out1 = ob_get_contents();

echo "World";

$out2 = ob_get_contents();

ob_end_clean();

var_dump($out1, $out2);
?>

The above routine will output:

string(6) "Hello "
string(11) "Hello World"


ob_flush() - Flush (send) the contents of the output buffer

ob_flush


(PHP 4 >= 4.2.0, PHP 5)

ob_flush — Flush (send) the contents of the output buffer

Description

void ob_flush (void)


This function will send the contents of the buffer (if there is content in it). If you want to further process the contents of the buffer, you must call ob_get_contents() before ob_flush(), because the buffer contents will be discarded after calling ob_flush().

This function will not destroy the output buffer, but functions like ob_end_flush() will destroy the buffer.

Return value

No return value.




ob_clean() - clear (erase) the output buffer

ob_clean

(PHP 4 >= 4.2.0, PHP 5)

ob_clean — clear (erase) the output buffer
Description

void ob_clean (void)


This function is used to discard the contents of the output buffer.

This function will not destroy the output buffer, while functions like ob_end_clean() will destroy the output buffer.
Return value

No return value.

ob_end_flush() - flush out (send out) the contents of the output buffer and close the buffer

ob_end_flush — flush out (send out) the contents of the output buffer and close the buffer
 代码如下 复制代码
while (@ob_end_flush());
?>
Description bool ob_end_flush (void) This function will send the contents of the top-level buffer (if there is any content in it) and close the buffer. If you want to further process the contents of the buffer, you must call ob_get_contents() before ob_end_flush(), because the buffer contents are discarded after calling ob_end_flush(). Note: This function is similar to ob_get_flush(), except that ob_get_flush() will return the contents of the buffer as a string. Return value Returns TRUE on success, or FALSE on failure. The reason for the error is first of all, there is not a functioning buffer at the time of the call, or the buffer cannot be deleted for some reason (maybe for special buffers Word). Error/Exception If the function fails, an E_NOTICE exception will be raised. Update log Version Description 4.2.0 Added Boolean return value. Example Example #1 ob_end_flush() example The following example shows an easy way to emit the buffer contents and close all output buffers:
The code is as follows Copy code
while (@ob_end_flush());<🎜> ?>

ob_end_clean() - clears (erases) the buffer and closes output buffering

ob_end_clean — clear (erase) the buffer and close output buffering
Description
bool ob_end_clean (void)

This function discards the contents of the top-most output buffer and closes this buffer. If you want to further process the contents of the buffer, you must call ob_get_contents() before ob_end_clean(), because the buffer contents will be discarded when ob_end_clean() is called.
Return value

Returns TRUE on success, or FALSE on failure. The reason for the error is first of all, there is not a functioning buffer at the time of the call, or the buffer cannot be deleted for some reason (maybe for special buffers Word).
Error/Exception

If the function fails, an E_NOTICE exception will be raised.
Update log

Version Description
4.2.0 Added Boolean return value.

Example

The following example shows a way to remove all output buffers:

flush() - Flush the output buffer 
The code is as follows
 代码如下 复制代码

Example #1 ob_end_clean() example
ob_start();
echo 'Text that won't get displayed.';
ob_end_clean();
?>

Copy code

Example #1 ob_end_clean() example
ob_start();

echo 'Text that won't get displayed.';

ob_end_clean();

?>



Usually ob_flush();flush() are used together Use ob_start() to send the output to the buffer instead of to the browser.

Then use ob_get_contents to get the buffer data.

The function ob_end_clean will clear the contents of the buffer and close the buffer, but will not output the content. At this time, a function ob_get_contents() must be used in front of ob_end_clean() to obtain the contents of the buffer. In this case, the content can be saved to a variable before ob_end_clean() is executed, and then the variable can be operated after ob_end_clean().
http://www.bkjia.com/PHPjc/629067.htmlwww.bkjia.com
truehttp: //www.bkjia.com/PHPjc/629067.htmlTechArticleThe article introduces the php functions ob_start(), ob_end_clean(), and ob_get_contents(). Friends in need can refer to it. . Usage of the following three functions ob_get_contents() - Returns the contents of the output buffer...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn