PHP output buffering - sounds like a bad idea, is it?

南楼画角 提交于 2019-11-29 01:07:24

From my experience, there isn't a significant impact on performance. I also can't find consistent answers on the subject -- some people claim that there is barely any hit against performance, while some say that there is a minor but significant effect. There is even a comment on php.net suggesting that buffering increases performance as compared to multiple output functions, not that I've verified that or anything.

I think the question of whether or not to buffer has more to do with the intended use of your application. Buffering makes a lot of sense if you want to compress the output before sending it, or if you want to control exactly when and where the output takes place in your code. Since it doesn't take that much effort to add buffering, you may as well try it out -- it should be relatively easy to remove it if you need to.

I think the opposite. Not buffering output is a bad idea unless you run into a situation where you really need it. For instance, a script that's going to create huge amounts of output.

In most cases, burning a bunch of programmer time to save some unknown quantity of (cheap) memory sounds like a waste of resources.

If you are in the situation where content is getting output before the headers, you'll need to stuff it in a buffer or else the page will error out that content was output before headers. This has happened to me with shared libraries and not enough time to go in and do a proper fix to get to launch. It's one of those mark a //TODO / FIXME and then go back and make it proper later on.

Using output buffering I was able to make a light weight templating system quickly for a home brewed MVC backend for my last PHP project. I love it and find it very useful.

And regarding resources: it's not that resource intensive. If you're worried about the little it uses, PHP is not the right tool for the job. I love PHP but it is NOT the lightest option. On any reasonably modern server though, that won't matter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!