Skip to main content

What is the purpose of the header() function?

Junior PHP
Quick Answer header() sends raw HTTP headers to the browser. Must be called before any output. Used for: redirects (Location header), setting content type (Content-Type: application/json), caching headers (Cache-Control), file download prompts (Content-Disposition). Always call exit after a redirect header to stop script execution immediately.

Answer

header() sends raw HTTP headers for redirects, JSON output, or content-type changes and must run before output.
S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice