0

Hi,

I read this ob_start() but want a bit cleaner explanation.
why we use it, should we use it, what happens if we don't?

I have never used it and won't use it until I understand it is best practise.

Thanks in advance

2
Contributors
2
Replies
7
Views
8 Years
Discussion Span
Last Post by veledrom
1

ob_start, as it indicates, will start output buffering. If you are using ob_start, then you should also use ob_end_flush, ie., flushing the output buffer.
I have used ob_start at places when I had already echo-ed something [which was unavoidable at that time] before starting the session or calling the header function [to get rid of the headers already sent... warning].
Here is an example of what I am talking about.

<?php
echo "hi<br>";
session_start();
echo "Bye<br>";
?>

This will throw an error, saying,

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

But this will not throw any error.

<?php
ob_start();
echo "hi<br>";
session_start();
echo "Bye<br>";
ob_end_flush();
?>

I have also seen/heard some people saying that using ob_start will cause overhead for the server as it has to keep the output in the buffer, until its flushed, but I am not sure how far it is true.
That's my 2 cents :)

0

Thanks for usefull info.

I remember I had strange header, header, header errors in Ubuntu when I run a project. Same project didn't give any errors in WinXP on Wamp.

Seems to it is all about header errors, maybe more than that.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.