PHP: Is Output Buffering Significantly Slower Than Strings?

Posted in Programming on August 8th, 2008 by Carl Zulauf

I love PHP. I use it constantly for rapid prototyping and small scripts that help “glue” things together for me. However, one annoyance I have with PHP is related to building large strings. This isn’t particularly difficult in PHP… in fact its easier in PHP than most languages. The problem is that sometimes my entire page output will need to be contained in a string for various reasons (headers still need to be sent, the output needs to be passed to a filter or other function, etc…). Creating multi-line strings in PHP using single or double quotes looks awful, and the heredoc syntax prevents proper indentation. All of these methods also require tons of concatenation if any logic is necessary. Sometimes I have forgone these methods and instead used Output Buffering to prevent output from being sent, then I simply grab the content of the output buffer and store it in a string. This has several advantages, including ease of coding and the freedom of using the same code whether I need the output stored in a string or sent to the client immediately. However, its main disadvantage is that it is much slower… right? Well, I decided to find out exactly how much slower Output Buffering really is. Read on for my results.
Read more »

Tags: ,