It is possible to delay the output of the headers to the browser until the entire php script has finished executing?
-
possible duplicate of [what is output buffering?](http://stackoverflow.com/questions/2832010/what-is-output-buffering) – Jon Mar 10 '11 at 16:27
-
http://php.net/manual/en/function.ob-start.php – KJYe.Name Mar 10 '11 at 16:27
-
4@Jon: **This is not a duplicate.** Output buffering is the answer yes, but these are **two completely different** questions. – Andrew Moore Mar 10 '11 at 16:31
-
1Agreed. Just because the answer is the same doesn't mean the question is. – Lightness Races in Orbit Mar 10 '11 at 16:37
-
1Output buffering as **A** answer, does not mean its the *correct* one – RobertPitt Mar 10 '11 at 16:51
-
1@RobertPitt: Yes, because complete project overhaul is always an option when trying to output headers in the middle of your work flow. – Andrew Moore Mar 10 '11 at 16:54
2 Answers
The headers will be sent once the first piece of code is sent from php to the webserver.
So you can just "not echo anything" until the end of the script or use output buffering
to achieve the same thing

- 38,542
- 15
- 125
- 143
This is the way most professional programmers would advise, the reasons you should always do it this way is that you can manage errors and error pages effectively.
If your application is already built to display output as the script is executed then i would advise you to start from scratch.
the way I usually manage output is with a small template's system, the template's system does not have to parse templates etc, it just needs to be passed a set of data and then include the required template.
you should create a class that acccepts data in the form of $template->set(key[,value = true])
and then a function that would display such as $template->display(filename)
, when this function executes you should extract the variables and then include the template file, after that has been done you then call exit(0)
so no further code is executed.
a simple template system can be like so:
class Template
{
private $_data = array();
public function set($key,$value = true)
{
$this->_data[$key] = $value;
}
public function display($template)
{
//Check template exuists
extract($this->_data);
require_once $template;
exit(0);
}
}
then use pretty simply like so:
$template = new Template();
$template->set("title","Practical home Page");
$template->set("header","My Home Page");
$lists = array(
array(
"value" => "list item 1",
"id" => "list_item_1",
"class" => "item"
),
array(
"value" => "list item 2",
"id" => "list_item_2",
"class" => "item"
)
);
$template->set("menu",$lists);
$template->display("templates/homepage.php");
you may also would like to read the following answer I had answered previously!

- 1
- 1

- 56,863
- 21
- 114
- 161
-
-1: Why? That's exactly how Zend Engine handles headers anyway. You can use [`header()`](http://php.net/header), [`headers_list()`](http://php.net/headers_list), and [`header_remove()`](http://php.net/header_remove) at any time to work with headers. Your method only adds a layer of complexity which will come to bite you in the rear if you forget a step... I'm sorry, but *"professional programmers"* don't add complexity for the sake of it. – Andrew Moore Mar 10 '11 at 16:53
-
2this is not complexity but structure and organization, and its not for the sake of its, its to help manage the separation of logic and view. using ob_start will not capture headers that have been sent, using ob_start is to capture output if within the output several headers have been sent then this is goes against standards of application architecture. view != logic – RobertPitt Mar 10 '11 at 17:03