If I understood your question correctly then I ask you to look at the value of output_buffering
directive in PHP.ini. The default value is 0 but the PHP installers often set it to 4096 (copying from php.ini-development or php.ini-production). This should be the case in your example i.e. the buffering is On.
Secondly, your code example has a flaw; it does not check the value returned by the headers_sent
function:
bool headers_sent ([ string &$file [, int &$line ]] )
Check the returned value first; consider the file and line parameters valid if the returned value is true:
<?php
$name = "tommy";
?>
<?php
$sent = headers_sent($filename, $linenum);
header("name: $name");
echo "tommy <br>";
if ($sent) {
echo "Headers sent in $filename on line $linenum";
} else {
echo "Headers not sent";
}
?>
Output:
tommy
Headers not sent
In summary:
- Your code does not fail because output buffering is on.
- Your assumption that your the headers were sent is wrong.
Note: if the output_buffering is set to 4096 then this works:
<?php
echo str_repeat(" ", 4095);
header("X-Foo: Bar");
echo ".";
But this fails with headers already sent error:
<?php
echo str_repeat(" ", 4096);
header("X-Foo: Bar");
echo ".";