Php functions ob_start (), ob_end_clean (), ob_get_contents () _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php functions: ob_start (), ob_end_clean (), and ob_get_contents (). This article introduces php functions ob_start (), ob_end_clean (), and ob_get_contents (). If you need them, refer to them. The usage of the following three functions: ob_get_contents ()-The articles in the returned output buffer introduce php functions ob_start (), ob_end_clean (), ob_get_contents (). If you need them, refer to them.

Usage of the following three functions

Ob_get_contents ()-return the content of the output buffer


Ob_get_contents

(PHP 4, PHP 5)

Ob_get_contents-return the content of the output buffer
Description
String ob_get_contents (void)

Only get the content of the output buffer, but do not clear it.
Return value

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

The code is as follows:

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 ()-extracts (Sends) the content in the output buffer.

Ob_flush

(PHP 4> = 4.2.0, PHP 5)

Ob_flush-extract (SEND) the content in the output buffer
Description
Void ob_flush (void)

This function sends the buffer content (if there is content in it ). To further process the content in the buffer, you must call ob_get_contents () before ob_flush () because the buffer content will be discarded after ob_flush () is called.

This function will not destroy the output buffer, but the buffer will be destroyed like the ob_end_flush () function.
Return value

No return value.


Ob_clean ()-Clear (erase) output buffer


Ob_clean

(PHP 4> = 4.2.0, PHP 5)

Ob_clean-clear (erase) output buffer
Description
Void ob_clean (void)

This function is used to discard the content in the output buffer.

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

No return value.

Ob_end_flush ()-extracts (Sends) the output buffer content and closes the buffer.


Ob_end_flush-extracts (Sends) the output buffer content and closes the buffer.
Description
Bool ob_end_flush (void)

This function sends the content of the top-level buffer (if there is content in it) and closes the buffer. To further process the content in the buffer, you must call ob_get_contents () before ob_end_flush () because the buffer content is discarded after ob_end_flush () is called.

Note: This function is similar to ob_get_flush (). The difference is that ob_get_flush () will return the content in the buffer as a string.

Return value

Returns TRUE if the call succeeds, or FALSE if the call fails. the first cause of the error is that there is no buffer that works during the call, or the buffer cannot be deleted for some reason (maybe for a special buffer ).
Error/exception

If the function fails, an E_NOTICE exception is thrown.
Update Log

Version description
A Boolean return value is added in 4.2.0.

Example

Example #1 ob_end_flush () example

The following example shows an easy way to send the buffer content and disable all the output buffers:

The code is as follows:
While (@ ob_end_flush ());
?>

Ob_end_clean ()-Clear (erase) the buffer and disable the output buffer.

Ob_end_clean-clear (erase) the buffer and disable the output buffer.
Description
Bool ob_end_clean (void)

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

Returns TRUE if the call succeeds, or FALSE if the call fails. the first cause of the error is that there is no buffer that works during the call, or the buffer cannot be deleted for some reason (maybe for a special buffer ).
Error/exception

If the function fails, an E_NOTICE exception is thrown.
Update Log

Version description
A Boolean return value is added in 4.2.0.

Example

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

The code is as follows:

Example #1 ob_end_clean () example
Ob_start ();
Echo 'text that won' t get displayed .';
Ob_end_clean ();
?>

Flush ()-refresh the output buffer

Usually ob_flush (); flush () is used together
Use ob_start () to output the same output to the buffer, rather than to the browser.
Then, use ob_get_contents to obtain the data in the buffer.

Ob_start () opens a buffer on the server to save all the output. Therefore, when echo is used at any time, the output will be added to the buffer until the program running ends or the end ends with ob_flush. Then the buffer content in the server will be sent to the browser for resolution and display by the browser.

The ob_end_clean function clears the buffer and closes the buffer, but does not output the content.
In this case, you must use the ob_get_contents () function to obtain the buffer content before ob_end_clean.
In this way, you can save the content to a variable before executing ob_end_clean (), and then perform operations on the variable after ob_end_clean.

Values (), ob_end_clean (), and ob_get_contents (). For more information, see. Usage of the following three functions ob_get_contents ()-return the output buffer...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.