header
Sends an HTTP response header.
Syntax
header(string $header, bool $replace = true, int $response_code = 0): voidParameters
| Parameter | Type | Description |
|---|---|---|
| $header | string | The header line: `Name: value`. |
| $replace | bool | Optional. Whether to replace a header of the same name. |
| $response_code | int | Optional. 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
- http_build_query — Builds a URL query string from an array.
- json_encode — Converts a value to a JSON string.
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.