It doesn't really have anything to do with php. The basics is that your browser expects headers first, content later.
Now you sent headers only once. So if you do an action that consist mainly of sending headers (like header()
) it should be obvious that this should be done first.
If you already sent headers (implicitly, because you started output), you can't send them again.
Bottomline is that if you want to control your headers, you have to do that first thing. It's like wanting to change the intro after the show has allready started. If you use the ob_start
functions you are saving all output to a cerain point. I would not recommend this; Make your program flow so you know what headers you want to sent, and send them first. Then call the functions that produce output.