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

Recommended Answers

All 2 Replies

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 :)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.