PHP | header_sent() Function

PHP header_sent() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP header_sent() Function.

The PHP header_sent() function checks if or where headers have been sent.

Syntax

Following below is the syntax to use this function -

bool headers_sent ([ string &$file [, int &$line ]] )


Parameter Details

Sr.NoParameters & Description
1

file

It contains the information about file or line parameters

2

Line

It contains the line number where output has started.


Return Value

This built-in PHP function TRUE if headers have already been sent, otherwise FALSE.

Example

Try out the following example below -

<?php
   if (!headers_sent()) {
      header('Location: http://www.webdesigntutorialz.com/');
      exit;
   }
   
   if (!headers_sent($filename, $linenum)) {
      header('Location: http://www.webdesigntutorialz.com/');
      exit;
   } else {
      echo "Headers already sent in $filename on line $linenum\n" .
         "Cannot redirect, for now please click this <a " .
         "href = \"http://www.webdesigntutorialz.com\">link</a> instead\n";
      exit;
   }
?>

Output

The following example above will check whether headers have been sent or not, if it has sent, it shows a message otherwise it will send headers.


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the http_response_code() Function in PHP.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.