headers_sent()
headers_sent() — Checks if or where headers have been sent.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
Signature
headers_sent(string &$filename = null, int &$line = null): bool
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| &$filename | string | If the optional filename and line parameters are set, headers_sent() will put the PHP source file name and line number where output started in the filename and line variables. |
| &$line | int | The line number where the output started. |
Return value
headers_sent() will return false if no HTTP headers have already been sent or true otherwise.
Example
A runnable snippet:
// If no headers are sent, send one if (!headers_sent()) { header('Location: http://www.example.com/'); exit; } // An example using the optional file and line parameters // Note that $filename and $linenum are passed in for later use. // Do not assign them values beforehand. if (!headers_sent($filename, $linenum)) { header('Location: http://www.example.com/'); exit; // You would most likely trigger an error here. } else { echo "Headers already sent in $filename on line $linenum\n" . "Cannot redirect, for now please click this <a " . "href=\"http://www.example.com\">link</a> instead\n"; exit; }
See also
ob_start(), trigger_error(), headers_list(), header()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page headers_sent() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.