使用输出缓冲是否被认为是一种不好的做法?
php 程序员普遍认为 ob_start
/ ob_get_clean()
是不好的做法吗?
输出缓冲有什么缺点吗?
Are ob_start
/ ob_get_clean()
considered bad practice by php programmers in general?
Are there any disadvantages of output buffering?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这实际上是一个很好的做法。加快数据传输速度
It's actually a good practice. Speed up data transfer
在某些情况下,输出缓冲几乎是强制性的。使用 PHP,一旦您向用户输出某些内容,就会发送标头。因此,如果您在处理页面的过程中发生了需要发送标头的情况,除非打开缓冲,否则您将无法发送标头。否则,您会收到可怕的“无法修改标头信息 - 标头已发送”。
有些人会告诉你不应该那样编码。我说的是骗人的!
打开缓冲区后,您的代码可以更加灵活。
Output buffering in some circumstances is almost mandatory. With PHP as soon as you output something back to the user, headers are sent. Therefore if you get partway through processing a page and something happens that requires a header being sent you cant unless buffering is turned on. Otherwise you get the dreaded "Cannot modify header information – headers already sent".
Some will tell you you shouldn't code that way. humbug I say!
With buffers turned on your code can be more flexible.
输出缓冲并不是一个坏习惯。例如,它可以加快网站的加载速度通过使用 GZIP 压缩(尽管如果可能的话最好在 .htaccess 中执行此操作)。
缺点:不知道。好问题。
PS:我还发现了这个关于输出缓冲的主题。
output buffering is NOT a bad practice. For example it can speed up the loading of your website by using GZIP compression(although if possible it is better to do this inside .htaccess).
Disadvantages: I don't know. Good question.
P.S: also I found this topic about output buffering.
对于程序效率来说,输出缓冲区捕获还不错。这是 PHP 的一个有用的功能。然而,它可以用于用其他方法可以更好地完成的事情。代码异味可能是一个问题,因此请确保没有更好、更清晰的解决方案来解决您的问题。
For program efficiency, output buffer capturing is not bad. It is a useful feature of PHP. However, it can be used for things that are better done with other methods. Code smell can be an issue with it, so make sure that there's not a better, clearer solution to your problem.
它不被认为是坏的(或好的)。有些人喜欢,有些人不喜欢
我个人认为不使用它是有理由的。我认为这是最后的手段。有时,您可能会遇到输出缓冲可能是解决特定问题的唯一选择的情况,因此仅在这种情况下保存此选项。
我不认为使用它有任何性能提升或加速页面加载,但这也取决于您使用的服务器以及您是否使用 php 作为 mod_php 或者作为 cgi 或 fastcgi。
It's not considered bad (or good). Some people like it, some don't
Personally I think there are reasons not to use it. I think of it as a last resort. Sometimes you may have situation where output buffering may be your only choice to solve a particular problem, so save this option for just such situations.
I don't think there is any performance gain or speeding up of page load by using it, but it also depends on what server you using and if you using php as mod_php or as cgi or fastcgi.
输出缓冲的主要缺点是不知道(或不注意)缓冲区堆栈的深度。将其与过度激进的错误处理或意外退出/死亡的子例程结合起来,您将丢失缓冲区中的所有内容,几乎不留下任何关于正在发生的事情的线索。
例如,Zend 框架几乎对所有内容都使用输出缓冲,但是当遇到严重错误时,它会打印一条消息并立即退出。任何有用的调试信息都会丢失。
The main disadvantage of output buffering is not knowing (or paying attention to) how deep your buffer stack is. Combine this with overly aggressive error handling or subroutines that exit/die unexpectedly and you will lose whatever is in the buffer, leaving few clues as to what is going on.
For example, the Zend framework makes use of output buffering for almost everything, but when it hits a critical error it prints a message and exits immediately. Any helpful debugging information is lost.
如果我没记错的话,java也有这个输入和输出缓冲来读取和写入文件。
If I'm not mistaken java also have this input and output buffering to read and write file.
当然,输出缓冲意味着本来可以立即发送到浏览器的内容现在仍然保留在服务器上,这会占用额外的内存(如果您正在处理高可扩展性,这是一个非常重要的问题),因此如果您的程序正在使用执行一段时间后,这种内存开销会降低性能。
我不太了解 PHP,无法判断这是否属实,或者当您不使用缓冲时它是否会释放内存,但这通常是理论。
Surely output buffering means that content which could have been sent to the browser immediately is now sticking around on the server, which is taking up extra memory (a very important issue if you're dealing with high scalability) so if your program is then taking a while to execute, this memory overhead would impair performance.
I don't know PHP well enough to say if this is true or if it even frees the memory when you don't use buffering, but that is usually the theory.