gpt4 book ai didi

php - 当我使用多个 ob_start() 而不使用 ob_end_clean() 或 ob_end_flush() 时会发生什么?

转载 作者:行者123 更新时间:2023-12-03 00:59:39 24 4
gpt4 key购买 nike

我已经阅读了有关 ob_start() ob_end_clean() ob_end_flush() 的 php 手册。我已经看到了关于该主题的不同示例,无论如何我修改了该示例,但此时我很困惑。这是脚本。

ob_start();
echo "Hello x, ";

ob_start();
echo "Hello y, ";

ob_start();
echo "Hello z, ";

ob_start();
echo "Hello World";
$ob_2 = ob_get_contents();
ob_end_clean();

echo "Galaxy";
$ob_1 = ob_get_contents();
ob_end_clean();

echo " this is OB_1 : ".$ob_1;
echo "<br> and this is OB_2 : ".$ob_2;

该脚本的输出是:

你好 x,你好 y,我是 OB_1:你好 z,Galaxy

这是 OB_2:Hello World

--------------------------------------------------------

为什么输出不是这样的?

这是 OB_1:你好 x,你好 y,你好 z,Galaxy

这是 OB_2:Hello World

我错过了什么要点?

最佳答案

我将注释您的代码以解释发生了什么。所有输出缓冲区都初始化为空,这是标准的:

ob_start(); // open output buffer 1
echo "Hello x, "; // echo appended to output buffer 1

ob_start(); // open output buffer 2
echo "Hello y, "; // echo appended output buffer 2

ob_start(); // open output buffer 3
echo "Hello z, "; // echo appended to output buffer 3

ob_start(); // open output buffer 4
echo "Hello World"; // echo appended output buffer 4
$ob_2 = ob_get_contents(); // get contents of output buffer 4
ob_end_clean(); // close and throw away contents of output buffer 4

echo "Galaxy"; // echo appended to output buffer 3
$ob_1 = ob_get_contents(); // get contents of output buffer 3
ob_end_clean(); // close and throw away contents of output buffer 3

// at this point, $ob_2 = "Hello World" and $ob_1 = "Hello z, Galaxy"
// output buffer 1 = "Hello x," and output buffer 2 = "Hello y,"

echo " this is OB_1 : ".$ob_1; // echo appended to output buffer 2

// output buffer 2 now looks like "Hello y, this is OB_1 : Hello z, Galaxy"

echo "<br> and this is OB_2 : ".$ob_2; // echo appended to output buffer 2

// output buffer 2 now looks like:
// "Hello y, this is OB_1 : Hello z, Galaxy<br> and this is OB_2 : Hello World"

// output buffer 2 implicitly flushed by end of script
// output from buffer 2 captured by (appended to) output buffer 1
// output buffer 1 now looks like:
// "Hello x, Hello y, this is OB_1 : Hello z, Galaxy<br> and this is OB_2 : Hello World"
// output buffer 1 implicitly closed by end of script. This is when your output
// actually gets printed for this particular script.

关于php - 当我使用多个 ob_start() 而不使用 ob_end_clean() 或 ob_end_flush() 时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10441410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com