PHP http_clear_last_response_headers() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP 8.4+, http_clear_last_response_headers() clears the stored HTTP response headers from the last request made through PHP's HTTP wrapper.

After you read headers with http_get_last_response_headers(), call this when you want a clean slate so a later check does not reuse an older response.

Syntax

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

PHP
http_clear_last_response_headers();
CallWhat it does
http_clear_last_response_headers()Clears the stored last HTTP-wrapper response headers.
http_get_last_response_headers()Usually returns null after a clear.
function_exists("http_clear_last_response_headers")Checks that you are on PHP 8.4+ before calling.

Available since PHP 8.4. It clears the function-based store used by http_get_last_response_headers(). It does not unset the older $http_response_header variable if that variable exists in local scope.

Library declarations
PHP
http_clear_last_response_headers(): void

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
voidNo return value.

PHP Examples

Example 1 — Clear After Reading Headers

Show the PHP 8.4 pattern: read last headers, clear them, then confirm the store is empty. Sample values stand in for a real HTTP wrapper response.

PHP
<?php
if (!function_exists("http_clear_last_response_headers")) {
    echo "needs PHP 8.4+", PHP_EOL;
    exit;
}

// After file_get_contents("https://example.com") you might see headers, then:
http_clear_last_response_headers();
$after = http_get_last_response_headers();
var_dump($after);

How It Works

  1. function_exists() guards older PHP versions.
  2. http_clear_last_response_headers() wipes the stored wrapper headers.
  3. http_get_last_response_headers() then returns null.
  4. var_dump() prints NULL for that empty state.

Example 2 — Clear When Nothing Was Stored

Calling clear with no prior HTTP wrapper request is safe and still leaves the getter at null.

PHP
<?php
if (!function_exists("http_clear_last_response_headers")) {
    echo "needs PHP 8.4+", PHP_EOL;
    exit;
}

http_clear_last_response_headers();
echo http_get_last_response_headers() === null ? "empty" : "has headers", PHP_EOL;

How It Works

  1. No HTTP wrapper call was made in this demo.
  2. clear still runs without error.
  3. The getter returns null.
  4. We print empty to describe that state.

Example 3 — Reset Between Two Logical Requests

Illustrate clearing between checks so a second phase does not reuse the first response headers.

PHP
<?php
// Illustrative store for the tutorial View Output:
$store = ["HTTP/1.1 200 OK", "Content-Type: text/html"];
echo "before clear: ", $store[0], PHP_EOL;

$store = null; // same idea as http_clear_last_response_headers()
echo "after clear: ", $store === null ? "null" : "still set", PHP_EOL;

How It Works

  1. The first line represents headers left by an HTTP wrapper call.
  2. Clearing drops that stored list.
  3. A later get would see null until another request runs.
  4. Use the real http_* functions on PHP 8. 4+ in application code.

Frequently Asked Questions

On PHP 8.4+ it clears the stored HTTP response headers from the last HTTP wrapper request, such as one made with file_get_contents("http://...").
PHP 8.4 or newer. Older versions do not include this function.
No. It clears the store used by http_get_last_response_headers(). The older local variable is separate.
Call it after you finish inspecting headers, or before a new HTTP wrapper request when you want a clean null if the next request fails.
http_get_last_response_headers() returns null until another HTTP wrapper response is stored.

Next: http_get_last_response_headers()

+, http_get_last_response_headers() returns the HTTP response headers from the last reques.

http_get_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