1, background

has a code, because the method of encapsulation is invoked, so there will be some unnecessary return values. How can these return values be processed to make it disappear. Some people say, is it okay to clear the variable? But what if the return value can't be cleared up?

two, ob_start ()

1, concept:

, this function will open output buffer. When the output buffer is activated, the script will not output the content (except the HTTP header), but the content that needs to be output is stored in the internal buffer. The content of the

internal buffer can be copied into a string variable with the ob_get_contents () function. To output the content stored in the internal buffer, you can use the ob_end_flush () function. In addition, the ob_end_clean () function will silently discard the content of the buffer.

from PHP http://php.net/manual/zh/function.ob-start.php

2, buffer the basic usage of

 / / ob_start buffer (open); / / remove the contents of the buffer (ob_clean); / / washed out (sent) output buffer (ob_end_flush) content and close the buffer; 

three,

1,

Basic test of

 echo'1'(echo); ob_start;'2'; ob_clean; 

) style= "color: #ff00ff" > results: 1

explained: This is the most simple usage, that is to the outside do not want to output content with ob_start (); then, remove the contents of the buffer, so as to realize the use of our scene. We can selectively output what we want to output. Get rid of what you don't want to output.

2,

 echo'1'(echo); ob_start;'2'; / / output buffer (ob_end_flush); 

12

3 and

 results: / / open ob_start buffer (echo);'1'; / / clear the buffer, not at this time, 1 ob_clean output (output); / / buffer. But because it has cleared the buffer, so no output 1 ob_end_flush (echo);'2'

;

2

4 and

 results: (ob_start); / / open buffer of echo'1'; $a = ob_get_contents (); / / output buffer to $a, the equivalent of the assignment ob_clean (to $a); / / here to clear the buffer content echo'2'; / / 2 echo output $a output $a

results: 2 1

explained: ob_get_contents here () is to obtain the contents of the buffer. We can assign the obtained content to a variable, so that, even if the buffer is cleared, we can output a variable, or we can output a normal 1.

, four commonly used where the function used with the page static structure, micro web framework can play a key role, speak good render pages through the input buffer directly into a document, so the judgment of the file exists in the access to the user do not need to re render a page can be realized, can also be applied to large projects, good storage to render the page with NoSQL, the use of specific methods should be different, see your love which

above is a simple test to do my own. If you need it, you can compare the PHP manual and do some more tests.

This paper fixed link:http://www.script-home.com/php-using-ob_start-to-clear-out-the-output-and-selective-output.html | Script Home | +Copy Link

Article reprint please specify:PHP using ob_start () to clear out the output and selective output | Script Home

You may also be interested in these articles!