Difficulty: Easy
Correct Answer: The header() function sends raw HTTP headers to the browser, allowing you to control things like redirects, content type, caching, and other response headers before any output is sent.
Explanation:
Introduction / Context:
In web development with PHP, it is often necessary to control the HTTP response headers that the server sends to the browser. These headers influence how the browser interprets the response, handles caching, follows redirects, and deals with content types. PHP provides the header() function as a way to send custom HTTP headers before any body content is output. Understanding how and when to use header() is essential for implementing redirects, file downloads, and proper content type declarations.
Given Data / Assumptions:
Concept / Approach:
The header() function in PHP is used to send raw HTTP headers to the client. You can use it to set response codes (via Location for redirects or HTTP/1.1 status lines), specify Content-Type headers for JSON, XML, file downloads, or HTML, and control caching via Cache-Control and Expires. Because HTTP protocol rules state that headers must be sent before the body, PHP enforces that header() calls must occur before any actual output (echo, print, or HTML) has been sent, unless output buffering is used. The key concept is that header() operates at the HTTP header level, not at the level of HTML markup or database transactions.
Step-by-Step Solution:
Step 1: Recognize that HTTP responses have a header section and a body section, and that browsers read headers first to decide how to handle the response.
Step 2: Learn that calling header('Location: https://example.com') instructs the browser to redirect to another URL by sending a Location header and usually an appropriate status code.
Step 3: Understand that header('Content-Type: application/json') tells the browser that the response body should be interpreted as JSON instead of HTML.
Step 4: Realize that cache control headers like Cache-Control and Pragma can be sent via header() to influence how browsers and proxies cache the response.
Step 5: Conclude that header() is specifically designed to manipulate HTTP response headers, and must be called before output is sent so headers are formed correctly.
Verification / Alternative check:
If you inspect HTTP traffic using browser developer tools or a command line tool like curl, you can clearly see the headers produced by PHP when header() is used. For example, after calling header('Location: /home.php'), the response will contain a Location header in the raw HTTP output and the browser will navigate accordingly. Without header(), PHP would send default headers only, confirming that header() is the correct mechanism for custom header manipulation.
Why Other Options Are Wrong:
Option B is incorrect because including external PHP files is done with include or require, not with header(). Option C is wrong because database transactions are controlled by SQL statements or database specific PHP functions, not by HTTP response headers. Option D confuses the PHP header() function with HTML heading tags; header() does not generate any HTML markup at all.
Common Pitfalls:
A common pitfall is attempting to call header() after echoing output, which results in the PHP warning "Cannot modify header information - headers already sent". Another mistake is using header() for redirects without calling exit or die afterwards, which can inadvertently cause the rest of the script to execute. Developers should ensure that header() is called before output and that script flow is controlled properly when performing redirects or sending download headers.
Final Answer:
The header() function in PHP is used to send raw HTTP headers to the browser so you can control redirects, content types, caching, and other response header fields before any body output is sent.
Discussion & Comments