This time for you to bring PHP with Ob_start () clear output and selective output code sharing, PHP use Ob_start () to clear the output and selective output of the attention to what, the following is the actual case, take a look.
I. BACKGROUND
There is a piece of code because the encapsulated method is called, so there are some return values that would otherwise not be needed. What do you do with these return values to make it disappear? Some people will say, clear the variable is not good? But what if the return value cannot be cleared?
Second, Ob_start ()
1. Concept:
This function opens the output buffer. When the output buffer is activated, the script will not output content (except for the HTTP header), and the output will be stored in the internal buffer instead.
The contents of the internal buffer can be copied into a string variable using the Ob_get_contents () function. To output content stored in an internal buffer, you can use the Ob_end_flush () function. In addition, using the Ob_end_clean () function silently discards the contents of the buffer.
2. Basic usage of buffer
Open buffer Ob_start ();//Clears the contents of the buffer Ob_clean ();//Flushes out (sends out) the output buffer contents and closes the buffer Ob_end_flush ();
III. Basic Testing
1.
echo ' 1 '; Ob_start (); Echo ' 2 '; Ob_clean ();
Results: 1
Explanation: This is the simplest usage, which is to add Ob_start () to the outside of the content that you do not want to output, and then clear the contents of the buffer so that we can implement our usage scenario. We can choose the output that we want to output. Remove the one you don't want to export.
2.
echo ' 1 '; Ob_start (); Echo ' 2 ';//output buffer Ob_end_flush ();
Results: 12
3.
Open buffer Ob_start (); Echo ' 1 ';//clear buffer, no output 1ob_clean ();//Output buffer. But because the buffer has been cleared above, it will not output 1ob_end_flush (); Echo ' 2 ';
Results: 2
4.
Ob_start (); Open buffer echo ' 1 '; $a = ob_get_contents (); Output buffer content to $ A, equivalent to the assignment to $aob_clean (); Here clear the buffer contents echo ' 2 '; Output 2echo $a; Output $ A
Results: 2 1
Explanation: Here's ob_get_contents (); Is the contents of the fetch buffer. We can assign the obtained content to a variable, so that if we clear the buffer, we output the variable, or we can output the normal 1.
Four, the common place
This function is commonly used with the page static, the construction of micro-web framework can play a key role, the rendering of a good page through the input cache directly written to a file, so that the existence of the file to determine whether the user this visit need not to re-render a page, can also be applied to large projects, Use NoSQL to store rendered pages, depending on how you like them.
This is the simple test I made myself. If you need to, you can do more testing according to the PHP manual.
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
CI Framework (CodeIgniter) Operation Redis Step resolution
PHP implementation of statistics in binary 1 number of algorithm steps in detail