Really strange, at first glance, behavior. but! Inside pkhp there is a buffering, therefore cookies were put faster, than buffer was filled. There is an assumption that the setting in php.ini is responsible for this:
output_buffering = 4096
Because if you add such a piece before displaying cookies:
$file='';$line=''; echo headers_sent($file,$line); echo " file = $file ; line = $line"; for ($i = 1; $i < 500; ++$i) echo 'Hello world!'; echo "<br/>"; setcookie('sdfsdf',234); var_dump($_COOKIE);
it will be the expected Headers already set...
because there will be more data than 4096 specified in the default settings.
Zzyzh empirically found that this is the case;)
for ($i = 1; $i < 4000; ++$i) echo 'H';
here the cookies will be set, set the counter limit more by a few units (it is necessary to calculate how much, taking into account the other outputs), so that the buffer is filled and the output is realized before the cookies.