This article introduces php functions ob_start (), ob_end_clean (), and 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: |
Copy code |
Example #1 A simple ob_get_contents () example <? Php 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: |
Copy code |
<? Php 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: |
Copy code |
Example #1 ob_end_clean () example <? Php 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.