Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Hi!

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:

<?php
    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!