Home > Article > Backend Development > Detailed explanation of output buffer control (Output Control) in PHP
This article will take you to understand the output buffer control (Output Control) in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
In PHP, when we directly echo or print_r, the output content will be printed directly. However, in some cases, we do not want to print directly. At this time, we can use the output buffer control to control the output printing. Of course, this set of functions is not limited to printing content, we can also perform other operations, which we will discuss at the end.
First, let’s take a look at not allowing things like echo to print output.
ob_start(); echo 111, PHP_EOL; echo "aaaa", PHP_EOL; ob_end_clean();
I believe many friends should have seen the ob_start() function. Its function is to start a period of output buffer control. The output statements in the code after ob_start() will enter the output buffer. At this time, if we call ob_end_clean(), ob_clean() or ob_get_clean(), there will be no output. The function of all three of them is to clear the contents of the output buffer. For specific differences, you can refer to the function description or official documentation given at the end of the article.
ob_start(); echo 111, PHP_EOL; echo "aaaa", PHP_EOL; $v = ob_get_contents(); ob_end_clean(); echo $v;
As mentioned above, using ob_end_clean() will clear the contents of the output buffer, but in this code , we use the ob_get_contents() function to directly assign the contents of the buffer to the variable \ob_start();
php_info();
$v = ob_get_contents();
ob_end_clean();
echo $v;
The content in $v is the content of php_info(). This is the second capability of output buffer control.
Refresh (output) buffer content
ob_start(); echo 111, PHP_EOL; echo "aaaa", PHP_EOL; flush(); ob_flush();Similarly, if we want to directly output the content again in the buffer, use flush(), ob_flush(), Ob_end_flush() and ob_get_flush() are enough. In fact, it is equivalent to making the echo output statements after ob_start() revalidate and output normally.
ob_implicit_flush(); ob_start(); echo 111, PHP_EOL; echo "aaaa", PHP_EOL;After using ob_implicit_flush(), we no longer need to manually call functions such as ob_flush() to refresh the buffer content.
Some detection functions
ob_start(); ob_start(); echo 123, PHP_EOL; echo ob_get_length(), PHP_EOL; // 3 echo ob_get_level(), PHP_EOL; // 2 print_r(ob_get_status(true)); // Array // ( // [0] => Array // ( // [name] => default output handler // [type] => 0 // [flags] => 112 // [level] => 0 // [chunk_size] => 0 // [buffer_size] => 16384 // [buffer_used] => 0 // ) // [1] => Array // ( // [name] => default output handler // [type] => 0 // [flags] => 112 // [level] => 1 // [chunk_size] => 0 // [buffer_size] => 16384 // [buffer_used] => 17 // ) // ) ob_get_flush();ob_get_length() will return the length of the content in the current buffer. Here we only print a 123 and save 3 in the buffer. characters, so the output is exactly 3. ob_get_level() returns the level of the current buffer. Please note that we called ob_start() twice above, which means there are two levels of buffers. This buffer can be nested. The ob_get_status() function is the status information of the buffer. For field descriptions, you can view the official documentation and will not go into details here.
Use the callback function of ob_start() to replace the content of the output buffer
ob_start(function($text){ return (str_replace("apples", "oranges", $text)); }); echo "It's like comparing apples to oranges", PHP_EOL; ob_get_flush(); // It's like comparing oranges to oranges
Add URL rewriter
output_add_rewrite_var('var', 'value'); // some links echo '<a href="file.php">link</a> <a href="http://example.com">link2</a>'; // <a href="file.php?var=value">link</a> // <a href="http://example.com">link2</a> // a form echo '<form action="script.php" method="post"> <input type="text" name="var2" /> </form>'; // <form action="script.php" method="post"> // <input type="hidden" name="var" value="value" /> // <input type="text" name="var2" /> // </form>Do you see any clues in the above code? That's right, using the output_add_rewrite_var() function, we can add a parameter to the HTML link or form code when PHP outputs. Do you have any use scenarios in mind? Prevention of CSRF attacks on POST forms.
关于输出缓冲控制这块还有很多好玩的东西,不过限于篇幅我们先介绍到这里,将来踫到什么好的功能的应用我们再单独讲解。现在基于 Swoole 的应用越来越多,当我们需要将 TP 、 Laravel 这类传统框架转换成支持 Swoole 的时候,往往就需要在入口文件使用输出缓冲控制来进行修改。因为传统框架基本都是直接进行 echo 之类的输出的,而在 Swoole 中,echo 这类的内容是直接打印在控制台的,这就需要我们通过 ob_get_contents() 能力获得全部的输出再通过 response->end() 来进行实际的响应。另外,还有一些其他的场景也会用到输出缓冲控制:
最后,再给出输出缓冲控制相关的函数说明,具体内容大家还是要多看官方文档的介绍。
测试代码: https://github.com/zhangyue0503/dev-blog/blob/master/php/202005/source/%E8%BF%98%E6%90%9E%E4%B8%8D%E6%87%82PHP%E4%B8%AD%E7%9A%84%E8%BE%93%E5%87%BA%E7%BC%93%E5%86%B2%E6%8E%A7%E5%88%B6%EF%BC%9F.php
推荐学习:《PHP视频教程》
The above is the detailed content of Detailed explanation of output buffer control (Output Control) in PHP. For more information, please follow other related articles on the PHP Chinese website!