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;
Refresh (output) buffer content
ob_start(); echo 111, PHP_EOL; echo "aaaa", PHP_EOL; flush(); ob_flush();
ob_implicit_flush(); ob_start(); echo 111, PHP_EOL; echo "aaaa", PHP_EOL;
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();
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>
关于输出缓冲控制这块还有很多好玩的东西,不过限于篇幅我们先介绍到这里,将来踫到什么好的功能的应用我们再单独讲解。现在基于 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!