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!