为什么我没有收到 "headers already sent" 错误?

Why I don't get "headers already sent" error?

我有这样的代码:

<!DOCTYPE html>
<?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    $cookie_name = "user";
    $cookie_value = "John Doe";

    setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

?>
<html>
    <body>

        <?php
        if (!isset($_COOKIE[$cookie_name])) {
            echo "Cookie named '" . $cookie_name . "' is not set!";
        } else {
            echo "Cookie '" . $cookie_name . "' is set!<br>";
            echo "Value is: " . $_COOKIE[$cookie_name];
        }
        ?>

    </body>
</html>

根据我的知识应该return警告

Cannot modify header information - headers already sent by

如本题所述How to fix "Headers already sent" error in PHP 但我没有收到任何警告并且 cookie 已设置。这是为什么? php 是否添加了某种缓存,现在您可以在发送文本后发送 headers ?我正在使用 php 5.6.11.

如果您在 php.ini 中使用 output_buffering = On,您可以在发送 headers 之后发送 cookie。

这在 php.ini 评论中注意到:

Output buffering allows you to send header lines (including cookies) even after you send body content, at the price of slowing PHP's output layer a bit. You can enable output buffering during runtime by calling the output buffering functions. You can also enable output buffering for all files by setting this directive to On. If you wish to limit the size of the buffer to a certain size - you can use a maximum number of bytes instead of 'On', as a value for this directive (e.g., output_buffering=4096).