Content · Programming · PHP

PHP headers_list() Function: Usage and Examples

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

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

Use headers_list() when you need to returns the headers PHP has scheduled for the HTTP response in a PHP application. The examples below show the actual operation and its limitations.

What Is headers_list() in PHP?

Returns the headers PHP has scheduled for the HTTP response. The exact signature supported by your installation depends on its PHP version and enabled extensions.

Its syntax is:

headers_list(): array

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

header("X-Demo: sample"); echo is_array(headers_list()) ? "List available" : "Unavailable";

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 count(headers_list()) >= 0 ? "Headers inspected" : "Error";

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()) header("X-Trace: test"); foreach (headers_list() as $headerValue) { if (str_starts_with($headerValue,"X-Trace:")) echo $headerValue; }

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

Headers visible in CLI are not representative of a real web-server response.

Comparison With Related Functions

ApproachDifference
headers_list()Returns the headers PHP has scheduled for the HTTP response.
Related operationheaders_sent() reports whether output headers have been sent; headers_list() lists pending headers.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the function-specific limitationHeaders visible in CLI are not representative of a real web-server response.
Confusing this function with a related operationheaders_sent() reports whether output headers have been sent; headers_list() lists pending headers.

Key Takeaways

  • Returns the headers PHP has scheduled for the HTTP response.
  • Headers visible in CLI are not representative of a real web-server response.
  • headers_sent() reports whether output headers have been sent; headers_list() lists pending headers.

Use headers_list() 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.