Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

header

Sends an HTTP response header.

Syntax

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

Parameters

ParameterTypeDescription
$headerstringThe header line: `Name: value`.
$replaceboolOptional. Whether to replace a header of the same name.
$response_codeintOptional. The response status code.

Return value

voidThe function returns nothing.

Examples

A redirect to another page:

<?php
 
header('Location: /login');
exit;

A JSON response:

<?php
 
header('Content-Type: application/json; charset=utf-8');
 
echo json_encode(['ok' => true]);

Notes

Headers must be sent before any output: even a space before <?php raises a warning. End the program with exit after a redirect.

See also

Related recipes

In the tutorial

  • HTTP requests and responses — How PHP receives a request and builds a response — the request method and address, headers, status codes, redirects and JSON responses.

Thanks for reading!

Donate to the site — we work hard for you.