From PHP to browser caching mechanism, have to look!

Source: Internet
Author: User
Tags browser cache

All PHP programmers know that echo "1" is executed in the PHP script, and "1" is displayed in the guest's browser.

However, when we execute the code below, we do not display "2" after "1" for 5 seconds, but wait 5 seconds to display "12" directly.

Echo ' 1 '; Sleep (5); Echo ' 2 ';


This involves a few caching mechanisms, for a higher salary, students are very necessary to this caching mechanism to learn well.

Typically, our web application consists of the following elements:

Php->apache-> Browser. In this article, we will use this architecture as an example to explain how the data flows through the "chain".

Looking at the above figure, we finally know why the above will show "12", because Echo ' 1 ' is not loaded with PHP cache, so "1" is still in the PHP cache, not to the browser, wait until the end of the program "12" to go to the browser.

Of course we can also manually refresh the cache

Echo ' 1 '; Ob_flush () // write PHP cache to APAHCE cache Flush // write Apahce cache to browser cache Sleep (5); Echo ' 2 ';

After we change the code to the above, the browser still has to wait 5 seconds to display "12", because "1" has been sent to the browser, but the browser cache is not full, and no rendering, until the end of the program to render "12."

We take the Google browser as an example (cache 1000bytes), through the following code, we can achieve the first display "1", 5 Seconds to display "2"

Echo str_repeat // This will fill the browser cache Echo ' 1 '; Ob_flush () // write PHP cache to APAHCE cache Flush // write Apahce cache to browser cache Sleep (5); Echo ' 2 ';

In this case, we have to mention the "Ob_start ()" function, the function of which is to open a new PHP cache, but this cache far more than the 4096,php document describes that this cache is large enough. We still use the code to illustrate

Ob_start ();
echostr_repeat// This will fill the browser cache with echo ' 1 '; Ob_flush () Flush // write Apahce cache to browser cache Sleep (5); Echo ' 2 ';

On the basis of the original we only added a Ob_start (), the result turned into wait 5 seconds after the simultaneous display of "12". This is because each ob_start () in the original cache space to open up a sub-cache space,Ob_flush () is the current cache space output to the upper cache space, PHP only a cache space, the upper cache space is Apache cache, When PHP has more than one cache space,Ob_flush () cannot write the PHP cache to the Apache cache. We still use the image to understand:

From PHP to browser caching mechanism, have to look!

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.