Fixing the Headers Already Sent Error in PHP
Since we finally understand why PHP sends the Header error and why you cannot modify headers after output has been sent, what are the methods in which we can fix the header errors in PHP? Well the first method is the most obvious, remove the white space. If you have the white space before the <?php, remove it. This should solve the header error in PHP (unless you also output data aside from that). The next method is the second obvious, display output after processing. What this means is that instead of doing echo's throughout your script you should try to store most output in a string and display it at the end of the script. This way you can send headers at any point, this method is particularly useful when you are doing form validation and want to redirect to a error page or a "thank you" page. Here is an example of storing output in a string and displaying it after processing:
Solution 1:
1 2 3 4 5 6 7 8 9 | <?php $output = ""; $output .= "Some output"; // Note we do not echo the output till later header("Location: someLocation.php"); // this will not output as we are redirected, // but will also not cause a header error. echo $output; ?> |
Outputting the data after processing is my perferred method. It just makes things easier as you control when the data gets sent out and do not have to worry about anything messing up prior, plus it allows for string manipulation if you need it.
The final method that I know of, which is looked upon as a "band-aid" fix to the main issue is using Output Buffering. What output buffering essentially does is collect the data that you are outputting throughout the script into a buffer which you can grab into a variable, or just "flush" the data to the screen when you want to. Why is this method a "band-aid" method, well because you are just implementing output buffering instead of finding and solving the real problem. I am not saying output buffering does not have it's uses, but to use it strictly to solve the header problem is not really the reason Output Buffering was implemented, but all the same it can work. So let's take a look at the output buffering fix:
Solution 2:
1 2 3 4 5 6 7 8 | <?php ob_start(); // tell server we are buffering output (holding till later) echo "output1"; // below will not cause an error as the output is being held by PHP header("Location: someLocation.php"); ob_end_flush(); ?> |
As you can see Output Buffering still looks neat, and can even be applied using .htaccess (if using Apache). But as stated, it is far better to fix the root of the actual problem instead of relying on output buffering for the reason of solely fixing a Headers already sent PHP error.
Ending Notes
Headers already sent by output is a very common PHP error, and hopefully reading through you are able to figure out where your error lies and how to fix it. After you fix the error a few times you will learn which method you like best and hopefully will not have the error occur again, but hey if it does you know where to look to re-find the answer!
