How to solve: “Cannot modify header information – headers already sent”
In this article we will learn how to solve: “Cannot modify header information – headers already sent” which is a very common error in PHP projects.
Hey you programmer, ok? Let’s learn more about PHP and how to solve the headers already sent error!
This error happens because there is some output being done before the headers, which causes the error
That is, the functions that send or modify headers, have to be called before anything else
Functions that can modify the header are:
And when we talk about output or output, it can be any other type of PHP function like a var_dump or HTML code
As well as ‘invisible’ characters on the page, which are accidentally added
See what can cause the headers already sent error:
<?php echo 'Hello'; header('some modification');
And this all happens because PHP can pass the headers, and it needs to pass, only once per request
And after a line break, caused by any of the reasons above, it won’t be able to add more to the header
Generating the error message, that the header has already been sent, why is that exactly what happened
So always make sure the functions that modify the header are first in the code
In this article we learned how to solve: Cannot modify header information – headers already sent”
We just need to ensure that functions that change or send something to the header are placed at the top of the code
Because even an HTML code can interrupt the sending of headers, causing you to receive this error
Want to learn more about PHP? Click here!