Content · Programming · PHP

PHP headers_sent() Function: Usage and Examples

Understand PHP headers_sent() with three examples, return behavior, important pitfalls, and related operations.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Use headers_sent() when you need to checks whether HTTP headers have already been transmitted in a PHP application. The examples below show the actual operation and its limitations.

What Is headers_sent() in PHP?

Checks whether HTTP headers have already been transmitted. The exact signature supported by your installation depends on its PHP version and enabled extensions.

Its syntax is:

headers_sent(&$filename = ..., &$line = ...): bool

Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.

Example 1: Basic Usage

<?php

var_dump(headers_sent());

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Example 2: Different Input or Behavior

<?php

echo headers_sent() ? "Headers sent" : "Headers available";

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Practical Example: Applying the Function

<?php

if (!headers_sent($sourceFile,$sourceLine)) { header("X-Demo: ready"); echo "Header set"; } else { echo "Headers sent at $sourceFile:$sourceLine"; }

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Important Behavior and Edge Cases

Once headers are transmitted you cannot safely add or replace them using header().

Comparison With Related Functions

ApproachDifference
headers_sent()Checks whether HTTP headers have already been transmitted.
Related operationheaders_list() lists scheduled headers; headers_sent() checks the sent state.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the function-specific limitationOnce headers are transmitted you cannot safely add or replace them using header().
Confusing this function with a related operationheaders_list() lists scheduled headers; headers_sent() checks the sent state.

Key Takeaways

  • Checks whether HTTP headers have already been transmitted.
  • Once headers are transmitted you cannot safely add or replace them using header().
  • headers_list() lists scheduled headers; headers_sent() checks the sent state.

Use headers_sent() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.

Share this resourceSend this page to someone who may find it useful.