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

2024-01-09

我已经查看了关于 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:你好世界

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

为什么输出不是这样的?

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

这是 OB_2:你好世界

我错过了什么?


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

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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当我使用多个 ob_start() 而不使用 ob_end_clean() 或 ob_end_flush() 时会发生什么? 的相关文章

  • 在 PHP 数组定义中显示重复键警告

    下面的代码是否可以得到警告 error reporting E ALL s array a gt 1 a gt 1 var export s 你唯一的希望 除了count 你自己 是你的编辑足够聪明 可以突出显示拼写错误 此屏幕截图来自 P
  • PHP 下载脚本输出损坏的文件

    我正在用 PHP 为我的 CMS 构建一个文件下载类 当时我注意到它以不同的编码格式输出文件 我尝试使用 readfile file get contents fread 但似乎都在做同样的事情 这就像与输出缓冲有关的东西 我使用脚本下载的
  • PHP-MySQLi 连接随机失败并显示“无法分配请求的地址”

    大约两周以来 我一直在处理 LAMP 堆栈中最奇怪的问题之一 长话短说 与 MySQL 服务器的随机连接失败并显示错误消息 Warning mysqli real connect HY000 2002 Cannot assign reque
  • YUI压缩机或类似的PHP?

    我一直在我的测试服务器上使用 yuicompressor jar 来动态最小化已更改的 JavaScript 文件 现在我已经将网站部署到公共服务器上 我注意到服务器的策略禁止使用 exec 或其等效项 因此我不再执行 java 有没有一个
  • PHP 删除字符最后一个实例之前的所有内容

    有没有办法删除某个字符之前的所有内容 包括最后一个实例 我有多个字符串 其中包含 gt e g the gt cat gt sat gt on gt the gt mat welcome gt home 我需要对字符串进行格式化 以便它们变
  • 禁用外部点击时关闭模式

    我正在制作一些使用模式的博客物质化 但我的模态 onclick 外部和错误数据有问题 这是我的代码 main js function changepassword var user userlog val var content conte
  • 随机错误 symfony:ContextErrorException: 警告: simplexml_load_file(): I/O 警告: 无法加载外部实体

    在我的 Symfony 项目中 当我进入应用程序中的随机页面时 会出现以下随机错误 ContextErrorException Warning simplexml load file I O warning failed to load e
  • PHP 电子邮件验证[重复]

    这个问题在这里已经有答案了 For PHP最好的电子邮件验证方法是什么preg NOT ereg因为它是已弃用 删除 I don t需要检查该网站是否存在 这不像最高安全性 我找到了很多方法ereg但它们 显然 不是好的做法 我建议你使用F
  • Memcache 不会刷新或清除内存

    我一直在尝试清除我的内存缓存 因为我注意到使用时存储占用了近 30 的服务器内存ps aux 所以我运行了以下 php 代码 memcache new Memcache memcache gt connect localhost 11211
  • php隐藏所有错误[重复]

    这个问题在这里已经有答案了 隐藏的最佳做法是什么allPHP 错误 因为我不想向用户显示错误 我尝试过使用 htacess通过输入代码php flag display errors off在那里 但它返回给我一个500 error 还有其他
  • Apache 子进程已退出,状态为 255

    经过大量的搜索 尝试 修复 等待和哭泣 在我放弃之前 我想为这个错误抓住最后的机会 我们正在奔跑Microsoft Windows Server 2012 Apache 2 4 6 Win64 OpenSSL 1 0 1e PHP 5 5
  • 在 PHP 中将整数转换为十六进制值

    如何将PHP中第一类中的数字转换为第二类中的数字 是否有内置函数来转换数字 也是我的标题 将整数转换为十六进制值 甚至正确 class Permission const READ 1 const UPDATE 2 const DELETE
  • 当路由不存在时重定向 laravel 4

    我正在使用 laravel 4 当我的项目处于生产模式时 我得到 抱歉 找不到您要查找的页面 当我到达一条不存在的路线时 当我 grep 我的代码时 它在两个地方找到 vendor symfony debug Symfony Compone
  • 彩色 var_dump() 和错误

    我怎样才能将样式设置为var dump 功能和PHP错误样式 如下图所示 目前我有下一个观点var dump with pre var dump pre 没有它将全部在一行中 并且只是纯文本的错误 我搜索了一些 PHP 颜色错误 var d
  • PHP 中的异或加密

    我是 Xor 加密的新手 并且在使用以下代码时遇到了一些问题 function xor this string Let s define our key here key magic key Our plaintext ciphertext
  • 自定义 WordPress 画廊 html 布局

    当使用默认媒体上传器在 WordPress 中创建图像库时 WordPress 将图像包装在一堆 HTML 标记中 如何在生成之前覆盖它 以便我可以输出所需的标记并更改创建图库布局的方式 目前 WordPress 生成的代码如下 div d
  • 将客户分配到 magento 的多个客户组

    您好 我想将多个组分配给特定客户 例如 Rajat 客户 属于 批发 零售商 电力 实际上我在上面看到了同样的话题每个客户有多个客户组 https stackoverflow com questions 6153011 multiple c
  • PHP-如何根据条件配对数组中的项目

    如何将数组中的项目配对 假设我有一个数组Fighters 我想根据他们的情况将他们配对Weights 体重最接近的拳手应作为配对最佳匹配 但如果他们是在同一个团队中 他们不应该配对 团队 1 战斗机A体重为60 战斗机B体重为65 2队 战
  • 创建单个随机 Magento 优惠券

    我遇到了一些麻烦 我想要做的是每次有人订阅我们的时事通讯时 在 Magento 中自动生成一个随机优惠券代码 这张优惠券可减 10 美元 并且有exp 订阅后两周的日期 因此 我正在尝试编写一个简单的脚本 当提交 订阅我们的时事通讯 表单时
  • PHP 中根据相似值对数组进行分组

    我有一个具有以下结构的数组

随机推荐