PHP headers_sent() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, headers_sent() tells you whether the response headers have already been sent to the client. After that point, header() and similar calls can no longer change them.

You can pass optional variables by reference to learn which file and line produced the first output that forced headers out. That makes "headers already sent" bugs much easier to find.

Syntax

Call headers_sent() with the values you need. Start with this line.

PHP
$sent = headers_sent($file, $line);
CallWhat it does
headers_sent()Returns true if headers were already sent, otherwise false.
headers_sent($file)Also fills $file with the path that caused the first output.
headers_sent($file, $line)Fills both the file path and the line number of the first output.

Call headers_sent() before you try header(), setcookie() or session start helpers that need clean headers. The file and line arguments are optional and passed by reference.

Library declarations
PHP
headers_sent(?string &$file = null, ?int &$line = null): bool

Parameters

ParameterDescription
fileOptional. A variable passed by reference that receives the PHP file path where output started.
lineOptional. A variable passed by reference that receives the line number of that first output.

Return Value

Return ValueDescription
booltrue if headers have already been sent, false if you can still change them.

PHP Examples

Example 1 — Check Before Sending a Header

Test headers_sent() and only call header() when it is still safe.

PHP
<?php
if (!headers_sent()) {
    header("X-Safe: 1");
    echo "header queued", PHP_EOL;
} else {
    echo "too late for headers", PHP_EOL;
}

How It Works

  1. At the start of the script, headers usually are not sent yet.
  2. The if condition passes, so header() can run.
  3. We print a short confirmation message.
  4. This guard prevents "headers already sent" errors in larger apps.

Example 2 — Capture File and Line After Output

Create output first, then ask headers_sent() for the file and line that triggered sending.

PHP
<?php
echo "body starts", PHP_EOL;

$file = null;
$line = null;
$sent = headers_sent($file, $line);

echo "sent=", $sent ? "yes" : "no", PHP_EOL;
echo "line=", (string) $line, PHP_EOL;

How It Works

  1. echo writes body output, which forces headers out on a normal SAPI.
  2. headers_sent($file, $line) returns true afterward.
  3. The line variable points near the echo that caused the send.
  4. Exact line numbers can differ slightly by SAPI; the idea is to locate the first output.

Example 3 — Fail Soft When Headers Are Late

Show a helper pattern that skips setcookie-style work when headers are already gone.

PHP
<?php
function queueDemoHeader(string $value): string
{
    if (headers_sent()) {
        return "skipped";
    }
    header("X-Demo: " . $value);
    return "queued";
}

echo queueDemoHeader("ok"), PHP_EOL;

How It Works

  1. The helper checks headers_sent() before calling header().
  2. Early in the request it returns queued.
  3. If called after body output, it would return skipped instead.
  4. Soft failure is safer than letting header() warnings bubble up.

Frequently Asked Questions

It returns true when response headers have already been sent, and false when you can still change them.
They are optional reference arguments. When headers were sent because of output, PHP fills in the file path and line number of that first output.
Call it before header(), setcookie(), setrawcookie() or anything else that needs to modify response headers.
Something already produced output: echo, HTML, whitespace, an error message, or a BOM at the start of a file.
No. It only means headers are gone. The body may still be writing.

Next: http_clear_last_response_headers()

+, http_clear_last_response_headers() clears the stored HTTP response headers from the las.

http_clear_last_response_headers() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful