ob_start()是否用于输出缓冲,以便头被缓冲而不发送到浏览器?我说的有道理吗?如果不是,那么为什么我们要使用ob_start()?
当前回答
可以把ob_start()看作是在说“开始记住通常会输出的所有内容,但还没有对它做任何事情。”
例如:
ob_start();
echo("Hello there!"); //would normally get printed to the screen/output to browser
$output = ob_get_contents();
ob_end_clean();
还有另外两个函数通常与它配对:ob_get_contents(),它基本上提供了自ob_start()打开缓冲区以来“保存”到缓冲区的任何内容,然后是ob_end_clean()或ob_flush(),它们分别停止保存并丢弃保存的内容,或停止保存并一次性输出。
其他回答
我喜欢:
ob_start();
echo("Hello there!");
$output = ob_get_clean(); //Get current buffer contents and delete current output buffer
可以把ob_start()看作是在说“开始记住通常会输出的所有内容,但还没有对它做任何事情。”
例如:
ob_start();
echo("Hello there!"); //would normally get printed to the screen/output to browser
$output = ob_get_contents();
ob_end_clean();
还有另外两个函数通常与它配对:ob_get_contents(),它基本上提供了自ob_start()打开缓冲区以来“保存”到缓冲区的任何内容,然后是ob_end_clean()或ob_flush(),它们分别停止保存并丢弃保存的内容,或停止保存并一次性输出。
以下内容在现有的答案中没有提及: 缓冲区大小配置 HTTP报头 和嵌套。
ob_start的缓冲区大小配置:
ob_start(null, 4096); // Once the buffer size exceeds 4096 bytes, PHP automatically executes flush, ie. the buffer is emptied and sent out.
上面的代码提高了服务器性能,因为PHP将发送更大的数据块,例如,4KB(如果没有ob_start调用,PHP将向浏览器发送每个echo)。
如果你开始缓冲没有块大小(即。一个简单的ob_start()),那么页面将在脚本的末尾发送一次。
输出缓冲不会影响HTTP报头,它们以不同的方式处理。然而,由于缓冲,你可以在输出发送后发送头,因为它仍然在缓冲区中。
ob_start(); // turns on output buffering
$foo->bar(); // all output goes only to buffer
ob_clean(); // delete the contents of the buffer, but remains buffering active
$foo->render(); // output goes to buffer
ob_flush(); // send buffer output
$none = ob_get_contents(); // buffer content is now an empty string
ob_end_clean(); // turn off output buffering
这里有很好的解释:https://phpfashion.com/everything-about-output-buffering-in-php
这里公认的答案描述了ob_start()做什么-而不是为什么使用它(这是被问到的问题)。
如别处所述,ob_start()创建一个缓冲区,输出写入其中。
但是没有人提到在PHP中可以堆叠多个缓冲区。看到ob_get_level()。
至于为什么....
Sending HTML to the browser in larger chunks gives a performance benefit from a reduced network overhead. Passing the data out of PHP in larger chunks gives a performance and capacity benefit by reducing the number of context switches required Passing larger chunks of data to mod_gzip/mod_deflate gives a performance benefit in that the compression can be more efficient. buffering the output means that you can still manipulate the HTTP headers later in the code explicitly flushing the buffer after outputting the [head]....[/head] can allow the browser to begin marshaling other resources for the page before HTML stream completes. Capturing the output in a buffer means that it can redirected to other functions such as email, or copied to a file as a cached representation of the content
不,你错了,但方向是合适的;)
output - buffering缓冲脚本的输出。这就是(简而言之)回声或打印后的一切。头部的事情是,他们只能被发送,如果他们还没有被发送。但是HTTP表示,头信息是传输的第一步。因此,如果你第一次(在请求中)输出某些内容,则发送头信息,并且不能设置其他头信息。
推荐文章
- 原则-如何打印出真正的sql,而不仅仅是准备好的语句?
- 如何从关联PHP数组中获得第一项?
- PHP/MySQL插入一行然后获取id
- 我如何排序一个多维数组在PHP
- 如何在PHP中截断字符串最接近于一定数量的字符?
- PHP错误:“zip扩展名和unzip命令都没有,跳过。”
- Nginx提供下载。php文件,而不是执行它们
- Json_encode()转义正斜杠
- 如何在PHP中捕获cURL错误
- 如何要求一个分叉与作曲家?
- 如何在php中创建可选参数?
- 在文本文件中创建或写入/追加
- 为什么PHP的json_encode函数转换UTF-8字符串为十六进制实体?
- 如何从一个查询插入多行使用雄辩/流利
- 在PHP单元测试执行期间,如何在CLI中输出?