In PHP, header() helps with runtime and application data. Adds or replaces an outgoing HTTP response header before the response body has been sent.
What Is header() in PHP?
The function or method has a specific purpose: Adds or replaces an outgoing HTTP response header before the response body has been sent. The following examples show the main call, one additional situation, and an application-style use.
Syntax:
<?php
header(string $header, bool $replace = ..., int $response_code = ...) : void
Parameters and Return Value
$header(string) — argument used by this API to perform its operation; required.$replace(bool) — argument used by this API to perform its operation; optional.$response_code(int) — argument used by this API to perform its operation; optional.
Example 1: Basic Usage
Adds or replaces an outgoing HTTP response header before the response body has been sent.
<?php
header("Content-Type: text/plain; charset=UTF-8");
echo "Hello";
This example demonstrates the central operation. The final output depends on the supplied input and, where relevant, the runtime or connected service.
Example 2: Handling a Different Case
<?php
header("Cache-Control: no-store");
echo "Private response";
The second example focuses on a different input or failure condition, so the behavior can be distinguished from the basic case.
Practical Example
<?php
header("Content-Type: application/json; charset=utf-8");
http_response_code(200);
echo json_encode(["status" => "ok"], JSON_THROW_ON_ERROR);
The practical example demonstrates how the same subject fits into a larger application. Use your own file paths, endpoints and validated inputs rather than copying sample settings directly.
Important Behavior and Edge Cases
- Adds or replaces an outgoing HTTP response header before the response body has been sent.
- Check the return value and exceptions for failure before using the result. If an extension or external system is required, the example only works when that dependency is available.
- Use the documented return type and handle false, null and exceptions according to the specific call.
Comparison With Related Functions
| Approach | Difference |
|---|---|
| header | Adds or replaces an outgoing HTTP response header before the response body has been sent. |
| http_response_code | Sets the response status rather than a named header. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring errors from header | Check the actual result and handle the expected error path. |
| Using the demonstration unchanged in production | Adapt file paths, configuration, authentication and permission rules to the application. |
| Treating input or returned data as trusted | Apply validation at input boundaries and context-aware escaping at output boundaries. |
Key Takeaways
- Adds or replaces an outgoing HTTP response header before the response body has been sent.
- Check the return value and exceptions for failure before using the result. If an extension or external system is required, the example only works when that dependency is available.
- Use the documented return type and handle false, null and exceptions according to the specific call.
Adds or replaces an outgoing HTTP response header before the response body has been sent. Use this approach when its capabilities and constraints match the application. Handle invalid data, failure conditions and resource cleanup explicitly.