How to Fix "Headers Already Sent" error in PHP
The “Headers already sent” warning is demonstrated by PHP once you apply the header function for outputting headers or perform the setcookie function for setting cookies after any content that is not within the PHP tag. This tutorial will show you the way to fix such an error. Probably, it is one of the most widespread errors in PHP.
First and foremost, it is necessary to assure that the functions sending or modifying HTTP headers should run before making any output.
Solving the Error
It is recommended to apply output buffering functions for buffering the output before it is sent as a chunk, in the end.
Here is how you should act:
<?php ob_start(); ?>
Hello World !!!
<?php setcookie("name", "value", 100); ?>
Again !!!
<?php ob_end_flush();
About Error Handling in PHP
The process of catching errors raised by the programme and then taking relevant actions is called error handling.
Error handling is an essential part of any programming language. The process of error handling in PHP is quite simple.
Several functions (for example, die()function) are used for dealing with PHP errors. Those functions allow defining your own error handling rules, as well as modifying the ways the errors may be logged. Also they help to modify and enhance error reporting for suiting your needs.
Also, there are logging functions. With them, you can send messages to other machines directly, to the system logs, and so on.