go to bug id or search bugs for
The message Warning: Cannot modify header information - headers already sent is one of the big time-wasters of our profession. All of the documentation on this message including hundreds of web-pages and forum discussions end up as just a long list of things that the program MIGHT have done that would cause this message to appear. There is no documentation of exactly how a programmer should go about identifying exactly what the, usually trivial, mistake was that triggered the early send of the headers. The message should include information that directly points at the program action that caused PHP to send the headers.
print ''; // empty string
Warning: Cannot modify header information - headers already sent because of backtrace to line 1.
Warning: Cannot modify header information - headers already sent at line 2.
Add a Patch
Add a Pull Request
> print ''; // empty string
> header("Content-Type: application/json");
just don't echo / print anything ebfore headers, it's that easy
> headers already sent is one of the big time-wasters
just don't output anything when you intend send headers after, easy
> All of the documentation on this message including
> hundreds of web-pages and forum discussions
just start your application with ob_start() and the problem is forever, easy
spam2 at rhsoft dot net has pointed out that the problems resulting from accidentally sending text can be avoided either by "just don't echo / print anything ebfore headers" or by wrapping everything in ob_start/ob_clean. But neither of these help the programmer to FIX the problem, they just hide it. The problem is that there is no clean easy way to FIND the errant output in any real-life web-site, because there can be MILLIONS of lines of code in a modern web-site, and that code is the work of multiple team members any one of which can make the small mistake. On the web-site which triggered this suggestion the output came out of a diagnostic routine that was four nested includes deep. Because PHP does not tell the programmer WHERE the errant print/echo statement is this required slapping hundreds of "if (headers_sent()) print "<p>" . __FILE__ . ' ' __LINE__;" statements throughout the code and then gradually narrowing in to find the offending print. This is a brute force debugging method which is inappropriate in 2019. Computers exist to serve humans, not the other way around.