PHP OB series functions in a detailed

Source: Internet
Author: User
Tags file info php class phpinfo setcookie

Introduction of related functions:
1, Flush: Flush the contents of the buffer, output.
function format: Flush ()
Description: This function is often used and is highly efficient.
2. Ob_start: Open Output buffer
function format: void Ob_start (void)
Note: When the buffer is active, all non-file header information from the PHP program is not sent, but is saved in the internal buffer.
In order to output the contents of the buffer, you can use the contents of the Ob_end_flush () or flush () output buffers.
3. Ob_get_contents: Returns the contents of the internal buffer.
How to use: String ob_get_contents (void)
Description: This function returns the contents of the current buffer and returns FALSE if the output buffer is not activated.
4. Ob_get_length: Returns the length of the internal buffer.
How to use: int ob_get_length (void)
Note: This function returns the length of the current buffer, and, like ob_get_contents, if the output buffer is not stimulated
Live. FALSE is returned.
5. Ob_end_flush: Sends the contents of the internal buffer to the browser, and closes the output buffer.
How to use: void Ob_end_flush (void)
Description: This function sends the contents of the output buffer (if any).
6. Ob_end_clean: Delete the contents of the internal buffer and close the internal buffer
How to use: void Ob_end_clean (void)
Description: This function does not output the contents of the internal buffer but deletes it!
7. Ob_implicit_flush: Turn absolute refresh on or off
How to use: void Ob_implicit_flush ([int flag])
Description: People who have used Perl know the meaning of $│=x, the string can open/close the buffer, and
The Ob_implicit_flush function is the same as that, the default is to close the buffer, open the absolute output, each script output is straight
Send to the browser, no longer need to call flush ()


Second, in-depth understanding:

1. About the Flush function:
This function appears in PHP3, is a very efficient function, he has a very useful function is to refresh the browser
Cache. Let's give a very obvious example of how flush works.
Example 2.
-------------------------------------------------------------------------------------
---------------------------------
for ($i = 1; $i <=, $i + +) print ("");
This sentence is very key, the structure of the cache so that its content can only reach a certain size to be exported from the browser
In other words, if the content of the cache does not reach a certain size, it will not be output until the program finishes executing. By
After testing, I found that the bottom limit of this size is 256 characters in length. This means that content received by the cache will be
The stream was sent out.
for ($j = 1; $j <=; $j + +) {
echo $j. "
”;
Flush (); This will cause the cache additions to be squeezed out and displayed on the browser
Sleep (1); Let the program "Sleep" a second, will let you see the effect more clearly
}
?>
-------------------------------------------------------------------------------------
---------------------------------
Note: If you open an absolute refresh by adding Ob_implicit_flush () to the program's header, you can no longer use flush in your program
(), the advantage of doing so is: improve efficiency!

2. About OB series functions:
I'd like to cite an example of my good friend y10k first:
Example 3.
-------------------------------------------------------------------------------------
---------------------------------

For example, you can use the server and client settings information, but this information will vary depending on the client, if you want to
What about saving the output of the phpinfo () function? There's no way to say it without buffer control, but there's a
The control of the buffer, we can easily solve:
-------------------------------------------------------------
Ob_start (); Open buffer
Phpinfo (); Using the Phpinfo function
$info =ob_get_contents (); Get the contents of the buffer area and assign it to $info
$file =fopen (' Info.txt ', ' W '); Open File Info.txt
Fwrite ($file, $info); Write information to Info.txt
Fclose ($file); Close File Info.txt
?>
-------------------------------------------------------------
Using the above method, you can save the Phpinfo information of different users, which in the past I am afraid there is no way to do! actually on
Face is the way to turn some "process" into a "function"!

-------------------------------------------------------------------------------------
---------------------------------
Perhaps now you have a certain understanding of the function of Ob_start (), the above example seems simple, but in fact already palm
The main point of using Ob_start () is grasped.
<1&gt: Use Ob_start to open the browser cache, which ensures that the contents of the cache are in your call to flush
(), Ob_end_flush () will not be output until it is finished (or the program is executed).
<2> Now you should know the advantages you have: You can use Header,setcookie after any output and
Session, which is a big feature of Ob_start, you can also use the Ob_start parameter after the cache is written, and then
Automatically run commands such as Ob_start ("Ob_gzhandler"), and our most common practice is to use ob_get_contents () to
To the content in the cache and then process it ...
When you want to
After the data has been output, the file header is then output. The output control function does not use header () or Setcookie (),
The file header information sent affects only those data blocks that resemble Echo () and PHP code.

Let's start with a simple example that gives you a general impression of the output control:
Example 1.
-------------------------------------------------------------------------------------
---------------------------------
Ob_start (); Open buffer
echo "hello\n"; Output
Header ("location:index.php"); redirect the browser to index.php
Ob_end_flush ();//output all content to the browser
?>
-------------------------------------------------------------------------------------
---------------------------------
All people who know about the header () function know that this function sends a file header to the browser, but if you are using the
This function already has any output (including null output, such as spaces, carriage returns, and line breaks) that will prompt an error. If I
We get rid of the first line of Ob_start () and execute the program, we find an error message: "Header had
All ready to send by "! But with Ob_start, there is no hint of error, because when the buffer is open, echo is behind
The characters are not output to the browser, but remain on the server until you use flush or ob_end_flush to output the
To not have any file header output error!
<3&gt: When the processing is complete, we can use various methods to output, flush (), Ob_end_flush (), and wait until the program finishes executing
After completion of the automatic output. Of course, if you use Ob_get_contents (), then you have to control the output mode.
###########################################################################################
First, static template technology

Introduction: The so-called static template technology is in some way, so that users on the client side is generated by the PHP HTML page
Surface. If the HTML page is not updated again, the program will no longer invoke the page when another user browses to it again.
PHP and related databases, for some of the more informative sites, such as Sina,163,sohu. A technique like this
The benefits of coming are very huge.

There are two ways I know to implement static output:
<1&gt. A template.inc.php class implementation that modifies Phplib by y10k.
<2> Use the OB series function implementation.
For the first method, because it is not the issue to be studied in this article, so don't repeat it.
Let's take a look at the concrete implementation of the second approach:
Example 4.
-------------------------------------------------------------------------------------
Ob_start ();//Open buffer
?>
Full output of PHP page
$content = Ob_get_contents ();//Get all the contents of the PHP page output
$fp = fopen ("output00001.html", "w"); Create a file, and open it, ready to write
Fwrite ($fp, $content); Write the contents of the PHP page to output00001.html, then ...
Fclose ($FP);
?>

PHP OB series functions in a detailed

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.