Ob_start () and ob_end_flush () are good partners that can control the output. As a pair, it is easy to understand, but it is hard to understand when the numbers of two of them increase.
View sourceprint? 01 <? Php
02 ob_start ();
03 echo 'level 1 <br/> ';
04 ob_start ();
05 echo 'level 2 <br/> ';
06 ob_start ();
07 echo 'level 3 <br/> ';
08 ob_end_flush ();
09 ob_end_flush ();
10 ob_end_flush ();
Obviously, the result is:
Level 1
Level 2
Level 3
When the program changes to ob_end_flush () and changes to ob_end_clean (), what do you think will happen? The descriptions of these functions are attached:
Ob_clean-clear (erase) output buffer
Ob_end_clean-clear (erase) the buffer and disable the output buffer.
Ob_end_flush-extracts (sends) the output buffer content and closes the buffer.
Ob_flush-extract (send) the content in the output buffer
Ob_start-enable the Output Control Buffer
View sourceprint? 01 <? Php
02 ob_start ();
03 echo 'level 1 <br/> ';
04 ob_start ();
05 echo 'level 2 <br/> ';
06 ob_start ();
07 echo 'level 3 <br/> ';
08 ob_end_clean (); // modify
09 ob_end_flush ();
10 ob_end_flush ();
Result:
Level 1
Level 2
You may think that ob_end_clean () will clear the output of the latest ob_start (). In fact, this statement is not comprehensive. Let's look at the example below.
View sourceprint? 01 <? Php
02 ob_start ();
03 echo 'level 1 <br/> ';
04 ob_start ();
05 echo 'level 2 <br/> ';
06 ob_start ();
07 echo 'level 3 <br/> ';
08 ob_end_clean (); // The first modification
09 ob_end_flush ();
10 ob_end_clean (); // The second modification
This time, nothing is output.
Isn't there an ob_flush () in the middle? It is reasonable to say that level2 is output.
In fact, the main reason for this is the output multi-level buffer mechanism. This program example has three ob_start (), which means that it has three buffer zones A, B, and C. In fact, the php program itself also has A buffer zone for final output, we call it F.
In this program, These buffers have A certain level. C-> B-> A-> F, the highest level of F is the final output buffer of the program, we will explain the above procedures.
At the beginning. F: null
View sourceprint? 1 ob_start ();
Create A buffer. A: null-> F: null
View sourceprint? 1 echo 'level 1 <br/> ';
The program has output, and the output enters the lowest buffer zone A: 'level 1 <br/> '-> F: null
View sourceprint? 1 ob_start ();
Create a buffer B. B: null-> A: 'level 1 <br/> '-> F: null
View sourceprint? 1 echo 'level 2 <br/> ';
The program has output, and the output enters the lowest Buffer Zone B: 'level 2 <br/> '-> A: 'level 1 <br/>'-> F: null
View sourceprint? 1 ob_start ();
Create A buffer C: null B: 'level 2 <br/> 'a: 'level 1 <br/> '-> F: null
View sourceprint? 1 echo 'level 3 <br/> ';
The program has output, and the output enters the lowest buffer zone C: 'level 3 <br/> '-> B: 'level 2 <br/>'->: 'Level 1 <br/> '-> F: null
View sourceprint? 1 ob_end_clean (); // The first modification
Buffer C is cleared and closed. B: 'level 2 <br/> '-> A: 'level 1 <br/>'-> F: null
View sourceprint? 1 ob_end_flush ();
Buffer B is output to buffer A at the upper level and closed. A: 'level 1 <br/> level 2 <br/> '-> F: null
View sourceprint? 1 ob_end_clean (); // The second modification to www.2cto.com
Buffer A is cleared and closed. At this time, the thing of buffer A has not been actually output to the final F, so the whole program has no output.
There are many other ob functions, but it is hard to understand these mechanisms. Attach other functions
Flush-refresh the output buffer
Ob_clean-clear (erase) output buffer
Ob_end_clean-clear (erase) the buffer and disable the output buffer.
Ob_end_flush-extracts (sends) the output buffer content and closes the buffer.
Ob_flush-extract (send) the content in the output buffer
Ob_get_clean-get the content of the current buffer and delete the current output.
Ob_get_contents-return the content of the output buffer
Ob_get_flush-clears (sends) the buffer content, returns the content in string format, and closes the output buffer.
Ob_get_length-return the length of the output buffer content
Ob_get_level-nesting level of the returned output buffer mechanism
Ob_get_status-obtains the status of all output buffers.
Ob_gzhandler-the callback function used in ob_start to compress the content in the output buffer. Ob_start callback function to gzip output buffer
Ob_implicit_flush-enable/disable absolute fl
Ob_list_handlers-list all output handlers in use.
Ob_start-enable the Output Control Buffer
Output_add_rewrite_var-Add URL rewriter values)
Output_reset_rewrite_vars-Reset the value of the URL rewrite (Reset URL rewriter values)