Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Adding a Contact Form Redirecting After a Form Submission

what is the function of header (); ?

Frankly, I didn't understood what is the function of header (); anyone help me please ?

1 Answer


When you request to view a web page, the browser does not only get the content, also gets something called HTTP Headers. These headers are the core part of the HTTP requests and responses and give to your browser important information such as the type of browser or device that is making the request, the time and date on which the request was made, which type of data will be presented on screen, the size of the file, image or document requested, and much more information that your browser needs or it is useful to present the data on your screen.

Imagine you've written this code in HTML:

<p>This is a beautiful and unusual paragraph.</p>

When you request display this HTML file you've written in your browser, it receives an HTTP header called "Content-type", this header describes to your browser the type of data that will be presented on your screen. In this case, the value of this header would be text/html (HyperText Markup Language), indicates to your browser that the type of data that will be presented on your screen is the standard markup language used to create web pages and displays it in that format, showing on your screen the text This is a beautiful and unusual paragraph.

But for some reason unrelated to the universe you want that your browser will display the HTML file in plain text, how could you do this? Simply by using the header() function that provides PHP, that allows you to send a raw HTTP header to your browser or client browser.

Let us see an simple example of its use:

    header('Content-type: text/plain');

<p>This is a beautiful and unusual paragraph.</p>

In this case, you'll be telling to your browser that the type of data you want to display on your screen is plain text and your browser would present the content of your HTML file as plain text, resulting in the following in your screen:

<p>This is a beautiful and unusual paragraph.</p>

This is just an example, the type of use that can be given to this function are varied, so I leave it to you to investigate and experiment with this function!

You can read more about the header() function in PHP: Function header.

I hope my explanation has helped you!