How to Make header() Function in PHP

Share This Post

The header() function is a built-in function in PHP that must send a raw HTTP header. The HTTP functions are those functions that manipulate info sent from one page to a different page.

Before any other output has been through. The PHP header() function sends an HTTP header to a client or browser in a raw form.

Before HTML or other output has been sent to a browser or client, raw data is sent with the request (mainly HTTP appeal) made by the server as header information.

HTTP header provides required information about the object and sent in the message body more correct, about the request and response. The first method is below.

# PHP program to describes header function

# Redirect the browser

# The below code does not get executed 
# while redirecting



This will redirect your location of header, i.e. redirect to the URL: which URL you gave between header("");

Please remember This header() must be called before any actual production sent, whether by HTML tags, blank lines in a file, or from PHP. This is a very common error to read the code with include, or require, functions, or different file access functions, and have spaces or empty lines that are produced before header() call. The alike problem exists when using a single PHP/HTML file.

Types Of Header:

$header: The header is a string in this parameter. In this article, I will explain two types of header calls.

The first header starts with a string such as “HTTP/”, which is used to reckon out the HTTP status and send the code. The second case of the header is the “Location:”. It is a compulsory parameter.

$replace: It is an optional parameter. It denotes the header should replace the previous or add a second header. The default value is True (will replace). If $replace value is False then it forces multiple headers of the same type.

$http_response_code: It is an optional parameter. It forces the HTTP response code to the specified value (PHP 4.3 and higher).

Return Values: This function doesn’t return any value.

The second method is Print_r; header function in PHP


// PHP program to describes header function

// Set a past date
header(" friends, 10 July 1999 3:11:00 am");
header("hello google!");

<p>Hello World!</p>

<!-- PHP program to display
header list -->


Hello World!

Array ( [0] => X-Powered-By: PHP/7.3.9 [1] => monday, 10 Jul 1999 03:40:00 PM [2] => PHP: programing )

header-phpRead more: How To Fix Undefine Index In PHP ( $_POST and $_GET )

Creating a header() function in PHP is actually quite simple. All you need to do is create a new file and name it header.php (or whatever you want to call it), and then add the following code to it:

function header($title, $heading) {
echo "<html>
<head> <title>$title</title> </head>";

echo "<body bgcolor='white'> <table border='0' cellpadding='10'>

<tr bgcolor='#CCCCCC'> <td align='center'> <font size='+2'>$heading</font></td></tr></table></head></html>


Related Posts

5 Reasons That Make MacBooks Good for Coding

Looking for a reliable laptop or computer for coding?...

Python for Loop Iteration: How to Easily Manipulate a List

One of the most powerful features of the Python...

How To Get First Characters of a String in Python

Way to find the first characters of a string...

How To Convert Tuple To String In Python

In this post, you will learn how to convert...

How To Convert String to Double in Python

Good to hear, that you want to convert a...

Python TypeError: String Index Out Of Range Solution

A string is an array of characters and part...
- Advertisement -spot_img