PHP header() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, header() sends a raw HTTP header to the client. You must call it before any body output, such as HTML, echo text or whitespace outside PHP tags.

Use it for Content-Type, Location redirects, custom X- headers and status-related lines. The optional replace flag controls whether a matching header is overwritten, and response_code can force an HTTP status when you send a header string.

Syntax

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

PHP
header("Content-Type: text/plain; charset=UTF-8");
CallWhat it does
header("Content-Type: text/html")Sets the response media type before body output.
header("Location: /thanks.php", true, 302)Sends a redirect and sets status 302.
header("X-Trace: a", false)Adds another X-Trace header instead of replacing the previous one.

Call header() before any output. Location: triggers a redirect status unless you already set 201 or a 3xx code. On CLI, headers_list() is often empty even after header() calls; web SAPIs show the queued headers.

Library declarations
PHP
header(string $header, bool $replace = true, int $response_code = 0): void

Parameters

ParameterDescription
headerRequired. The header line, such as "Content-Type: text/plain" or "Location: /home".
replaceOptional. true (default) replaces a previous header with the same name. false keeps both.
response_codeOptional. Forces an HTTP response code when the header string is not empty. Default 0 means leave the code unchanged.

Return Value

Return ValueDescription
voidNo return value. The header is queued for the response.

PHP Examples

Example 1 — Queue Content-Type and a Custom Header

Set two response headers, then list them with headers_list(). On a web SAPI you see the queued lines; CLI may show an empty list.

PHP
<?php
header("Content-Type: text/plain; charset=UTF-8");
header("X-Tutorial: demo");

foreach (headers_list() as $line) {
    echo $line, PHP_EOL;
}

How It Works

  1. header() queues Content-Type before any body text.
  2. A custom X-Tutorial header is added the same way.
  3. headers_list() returns the headers ready to send on a web SAPI.
  4. The output above is the illustrative web-SAPI listing; CLI often prints nothing.

Example 2 — Replace Versus Keep Duplicate Headers

Show how the replace flag changes whether a second header overwrites the first.

PHP
<?php
header("X-Color: red");
header("X-Color: blue");          // replaces by default
header("X-Tag: one");
header("X-Tag: two", false);      // keep both

foreach (headers_list() as $line) {
    if (str_starts_with($line, "X-")) {
        echo $line, PHP_EOL;
    }
}

How It Works

  1. The second X-Color call replaces the first because replace defaults to true.
  2. X-Tag is sent twice because the second call passes false.
  3. We filter headers_list() to X- lines for a short demo printout.
  4. On CLI the list may be empty; this sample matches a typical web SAPI.

Example 3 — Redirect With a Status Code

Send a Location header and set status 302 in one call. Listing shows the Location line that would go to the browser.

PHP
<?php
header("Location: /thanks.php", true, 302);

foreach (headers_list() as $line) {
    echo $line, PHP_EOL;
}

How It Works

  1. Location tells the client where to go next.
  2. The third argument sets HTTP status 302 for this redirect.
  3. In a real script you normally exit right after redirecting.
  4. headers_list() shows the queued Location line on a web SAPI.

Frequently Asked Questions

It queues a raw HTTP response header. You must call it before any body output so the server can still send headers.
Something was already written to the output, such as HTML, echo, a blank line, or a UTF-8 BOM before the <?php tag.
true (default) overwrites a previous header with the same name. false adds another header of that name.
Use http_response_code(404) when you do not need a full header() string. You can also pass response_code as the third argument to header().
Calls are accepted, but CLI usually has nothing useful in headers_list(). Header behavior matters for web SAPIs such as Apache or php-fpm.

Next: header_register_callback()

header_register_callback() registers a function that runs just before PHP sends the respon.

header_register_callback() 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