3

I wonder about buffering in php. I have read a lot of threads about this.

How to fix “Headers already sent” error in PHP

Why use output buffering in PHP?

I have read only about advantages, there was not informations about disadvantages of using buffering.

Ex.

Advantages of output buffering for Web developers

  • Turning on output buffering alone decreases the amount of time it takes to download and render our HTML because it's not being sent to the browser in pieces as PHP processes the HTML.
  • All the fancy stuff we can do with PHP strings, we can now do with our whole HTML page as one variable.
  • If you've ever encountered the message "Warning: Cannot modify header information - headers already sent by (output)" while setting cookies, you'll be happy to know that output buffering is your answer.

If output buffering provide only advantages, why I should turn it on manually by function ob_start() or in any other way?

Community
  • 1
  • 1
Kentakis15
  • 31
  • 1
  • Do you want to buffer your login pages? – Jeroen Heier Jan 31 '17 at 14:40
  • @JeroenHeier I dont understand, If I have bar on whole site where I put name of user from DB, whose is logged in, First I have to check if user is logged in, So I have to run start_session -> and check variables. Please define 'login page'. //EDIT On the other hand, If user want to log in, First I have to check that He is not logged in, so on login_page I should use start_session() – Kentakis15 Jan 31 '17 at 14:44
  • 2
    Disadvantages: memory usage; lack of control over buffer stack; may "lose" critical error messages; beware infinite loops in your code if you're buffering output; risk of corruption if your output isn't text/html (e.g. file downloads); site may appear less responsive; CLI scripts may well not want to buffer, but to display output directly – Mark Baker Jan 31 '17 at 14:54

1 Answers1

0

This is probably a primarily opinion-based question, which should not appear on Stack Overflow.

On the other hand, I will risk saying, that this question has one short and definite answer and probably everyone will agree with me. So, to answer your question:

It is because explicit is always better than implicit

Quoting Jon Skeet from here:

In general

  • Implicit: something is being done for you automatically.

  • Explicit: you've written something in the source code to indicate what you want to happen.

Community
  • 1
  • 1
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66