PHP Output Control

Source: Internet
Author: User

By default, the output of a string to the browser, after 3 stages of PHP buffer->tcp buffer-> Browser (some versions of IE browser also exist buffer)

PHP default is to open the output buffer, in php.ini can be configured output_buffering=4096 (4KB, a memory page), set the PHP output buffer size

flush-Refresh Output buffer (as I understand it, refresh the output TCP Bufer)
ob_clean-Empty (erase) the output buffer
ob_end_clean-Empty (erase) buffer and turn off output buffering
ob_end_flush-flushes out (sends out) the output buffer content and turns off buffering
ob_flush-Flushing out (sending out) the contents of the output buffer
ob_get_clean-Gets the contents of the current buffer and deletes the current output ease.
ob_get_contents-Returns the contents of the output buffer
ob_get_flush-brushes out (sends out) buffer content, returns content as a string, and closes the output buffer.
ob_get_length-Returns the length of the output buffer content

(PHP 4, PHP 5, PHP 7)

ob_start- Open Output control buffer

bool ob_start ([ callback $output_callback [, int $chunk_size [, bool $erase ]])

This function opens the output buffer. When the output buffer is activated, the script will not output content (except the HTTP header), instead the content that needs to be output is stored in the internal buffer

Since some versions of IE will have a browser buffer (256 bytes), the following code is run under Chrome browser

1. Ob_start Usage Instructions

Ob_start (' replace_content '); Echo ' Kevin Durant go to Golden State warriors! ' ; // David West go to Golden State warriors! function replace_content ($content) {    returnstr_replace  $content);}

And look at the code below.

 ob_end_clean  (); //  The system buffer must be closed  ob_start  (null , 1 for  ( $i  = 1;  $i  <=10;  $i  ++) {   $i , ' <br> '  // ob_flush ();  flush  (); //     output TCP buffering  sleep  (1 

PHP has an output buffer enabled by default, so call Ob_end_clean first to turn off the default output buffer. Since the call to Ob_start sets the buffer size to 1 bytes, this code is output every 1 seconds.

Look at the following code can also do this effect

 for ($i$i$i+ +)    {echo$i, ' <br>'    ; Ob_flush ();     Flush (); // output TCP buffering    Sleep (1);}

The default output buffer for the system is used, and the buffer size is the default of 4,096 bytes, so you must call Ob_flush to refresh the output PHP buffered content.

2. Output process

//php Default buffer F
1 Ob_start();//Buffer a2 Echo' Champion 1<br/> ';3 Ob_start();//Buffer B4 Echo' Champion 2<br/> ';5 Ob_start();//Buffer C6 Echo' Champion 3<br/> ';7 Ob_end_clean();8 Ob_end_flush();9 Ob_end_clean();Ten $str=ob_get_contents(); One Echo $str;

Operation Result:没有输出任何东西

The entire buffer can be treated as a stack, and a new buffer is created, and the new buffer becomes the top buffer of the stack. With the new content output, the output will be output to the buffer at the top of the stack.

This program buffer level:C->B->A->F

Initial F:null

1.ob_start();after running, create a new buffer a, at which time the entire buffer condition

A:null->f:null

2.echo ‘level 1<br />‘;after running

The content is output to buffer a, at which time the entire buffer condition

A: ' Level 1<br/> '-f:null

3.ob_start();after running, create a new buffer B, at which time the entire buffer condition

B:null A: ' Level 1<br/> ', f:null

4.echo ‘level 1<br /\>‘;after running

The content is output to buffer B, at which time the entire buffer condition

B: ' Level 2<br/> ', A: ' Level 1<br/> ', f:null

And so on, to create a buffer C, run to the 6.echo ‘level 3<br />‘; following time, the entire buffer condition

C: ' Level 3<br/> ', B: ' Level 2<br/> ', A: ' Level 1<br/> ', f:null

Then run 7.ob_end_clean(); , buffer c is emptied and closed, and the buffer condition

B: ' Level 2<br/> ', A: ' Level 1<br/> ', f:null

Then runs 8.ob_end_flush(); , the contents of buffer B are output to a buffer of the previous level and buffer B is closed. At this point the buffer condition

A: ' Level 2<br/> level 1<br/> '-f:null

Then run 9.ob_end_clean(); , buffer A is turned on and off. The content of a is not actually output to the buffer f is closed, and finally only F:null, so the program has no output.

You can use Ob_get_level () to get The nesting level of the output buffering mechanism

The difference between 3.ob_clean (), Ob_end_clean (), Ob_flush (), Ob_end_flush ()

At first it was really silly to say that Ob_end_clean () and Ob_end_flush () would close the current buffer, and Ob_clean (),Ob_flush () would not.

Echo ' champion1<br> '; Ob_start (); Echo ' champion2<br> '; Ob_end_clean (); Var_dump (ob_get_contents());

Echo ' champion1<br> '; Ob_start (); Echo ' champion2<br> '; Ob_enb_clean (); Var_dump (ob_get_contents());

I believe you can tell the result of the output.

When we use the GD library to output images, or Ajax interface output data before we should use Ob_clean, because Ob_end_clean will close the current output buffer,

It is clear that using the PHP output buffer program is more efficient.

PHP Output Control

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.