ob_flush 和 ob_end_flush 有什么区别?
我对 PHP 函数 ob_flush()
和 ob_end_flush()
感到困惑。 关于函数 ob_flush
手册 说
The buffer contents are discarded after ob_flush() is called.This function does not destroy the output buffer like ob_end_flush() does.
我对这里的丢弃
和销毁
这两个词感到困惑。即使缓冲区内容在 ob_flush()
的情况下被丢弃,它们也无法被访问,即使它们在 ob_end_flush()
的情况下被销毁,它们也无法被访问。 那么这两个函数有什么区别呢?
更新:
响应JamWaffles 答案 我不明白删除缓冲区中的所有内容但保留缓冲区与删除整个缓冲区(释放它)因为 PHP 没有指针的概念,并且您无法获取缓冲区的地址,因此无论您保留空缓冲区还是释放它都应该无关紧要
i am confused about the PHP functions ob_flush()
and ob_end_flush()
.
About the function ob_flush
the manual says
The buffer contents are discarded after ob_flush() is called.This function does not destroy the output buffer like ob_end_flush() does.
i am confused about the words discarded
and destroyed
here. Even if the buffer contents are discarded in case of ob_flush()
they cant be accessed and even if they are destroyed as in case of ob_end_flush()
they cant be accessed.
Then whats the difference between these two functions?
UPDATE:
In response to JamWaffles answer I dont understand the significance of deleting everything in the buffer but keeping the buffer vs deleting the whole buffer(freeing it) because PHP has no concept of pointers and you cant get the address of buffers so it should be immaterial whether you keep the empty buffer with you or you free it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为在这种情况下它们的意思是相同的。当您想要将页面的部分刷新到客户端时,使用ob_flush(),而
ob_end_flush()
刷新整个缓冲区,然后销毁缓冲区。 ob_flush() 的作用是删除缓冲区中的所有内容,但保留缓冲区本身,以便在 ob_flush() 后可以将更多数据放入其中> 打电话。我会尽力解释得更好。
废弃
假设我有一个漂亮的亮橙色塑料桶。这是我的缓冲区。然后我得到一些沙子,代表缓冲区的内容,并将缓冲区(桶)填满。然后我拿起这个装有沙子的桶并将其倒入沙坑中,这就是我的客户。您会发现沙子不见了,但水桶仍然存在。这就是
缓冲区内容被丢弃
的含义 - 缓冲区本身可以重新使用(再次用沙子填充)。从内存的角度来说,内存被清空但没有被释放,因此可以再次填充。销毁
现在,如果我们再次拿起桶,再次装满沙子,清空沙子,然后放火烧桶,因为我们不再需要它了,这称为销毁缓冲区;缓冲区中的数据消失了,但缓冲区本身也消失了。就内存而言,内存被释放以供其他用途。
OP 问道,如果没有指针,这在 PHP 中很重要吗?嗯,这取决于你想做什么。如果您正在处理一个长页面,并且想要(例如)将页眉和侧边栏发送到客户端,同时处理页面的其余部分以便在完成后发送,请使用
ob_flush()
。如果您想将某些内容刷新到客户端且之后不再有任何输出,请使用ob_end_flush()。
我以一种居高临下的语气说话绝对没有不尊重的意思。我想做一个类比,以使定义尽可能清晰。
I think in this case they mean the same thing.
ob_flush()
is used when you want to flush parts of the page to the client, whereasob_end_flush()
flushes the entire buffer, then destroys the buffer. Whatob_flush()
does is delete everything in the buffer, but keeps the buffer itself so more data can be put into it after theob_flush()
call.I'll try to explain better.
Discarded
Let's say I have a nice, bright orange plastic bucket. This is my buffer. I then get some sand, representing the contents of the buffer, and fill the buffer (bucket) up. I then pick this bucket with sand in it up and pour it into a sandpit, which is my client. You'll notice the sand is gone, yet the bucket remains. This is what is meant by
the buffer contents are discarded
- the buffer itself can be re-used (filled up with sand again). In memory terms, the memory is emptied but not freed, so it can be filled again.Destroyed
Now, if we take our bucket again, fill it up with sand once more, empty the sand out and then set fire to the bucket because we don't require it any longer, that's called destroying the buffer; the data in the buffer is gone, but so is the buffer itself. In memory terms, the memory is freed for other use.
Is this significant in PHP, without pointers, the OP asks? Well, it depends what you want to do. If you're processing a long page, and want to (for example) send the header and sidebar to the client while you process the rest of the page for sending after it's done, use
ob_flush()
.If you want to flush something to the client without any more output after it, use
ob_end_flush()
.I mean absolutely no disrespect in talking in a rather patronising tone; I wanted to make an analogy to make the definitions as clear as possible.
ob_flush 不关闭输出缓冲
ob_flush does not turn off output buffering
ob_end_flush() 显示缓冲区中的所有内容,然后销毁缓冲区。 ob_flush 做同样的事情,但不破坏缓冲区只是清除它。
ob_flush()
=ob_end_flush()
displays everything from the buffer, then destroys the buffer.ob_flush
does the same, but does not destroy the buffer just clears it.ob_flush()
=