Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Output control functions in PHP

  Peter        2012-06-15 10:11:58       6,391        0    

The Output Control functions in PHP allow you to control when output is sent from the script. This can be useful in several different situations, especially if you need to send headers to the browser after your script has began outputting data. The Output Control functions do not affect headers sent using header() or setcookie(), only functions such as echo and data between blocks of PHP code.

These output control functions include ob_start(0, ob_clean(),ob_get_contents(), etc. To be honest, I am always mess up these ob_xxx() functions, some of them have similar functions and some of them can be substituted with some combinations of other functions. Today I reviewed these functions again. I tried to find the relationship among different ob_xxx() functions. Here is what I have now.

ob_clean() :  It discards the contents of the output buffer but will not destroy the output buffer. My understanding is that the output buffer contents are cleared but the memory allocated is not freed. It can be used when before calling header() functions.

ob_end_clean() : It is similar to ob_clean(), the differences are:
  1. It discards the contents of the topmost output buffer
  2. It will free up the memory allocated to  the output buffer.
ob_end_flush() : It is ob_end_clean()+ (echo the buffered output to the client standard output).

ob_get_flush() : It is similar to ob_end_flush(), but it will return the buffered output as a string instead of echoing them to the client. It can be described as ob_end_clean()+(return buffered output string)

ob_flush() : Send the contents of the output buffer to the client.

ob_get_contents() : Return the output buffer as a string and don't clear the output buffer.

ob_get_clean() : It is ob_get_contents()+ob_end_clean();

Other ob_xxx() functions are not so confusing, so I don't describe them here. You can check the PHP Output Buffer Control manual to read the details and some examples there are also very helpful.

The above summary may not be very accurate, they are my understanding about the output control functions in PHP. If you find any mistakes I made, please correct it for me. Thank  you.

PHP  RELATIONSHIP  OUTPUT BUFFER  OB 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.