Deepak Narwal
Deepak Narwal

Reputation: 401

what is difference between header and include, where which one should be used

I am confused with two terms

  1. header ("Location:homepage_php");

  2. include("homepage.php");

I am guessing that header is used after checking password procedure and about include, you can use it anywhere. But i am not sure what is actual difference between them and at what place out of these two one should be used.

Upvotes: 13

Views: 10169

Answers (8)

Bipin Maharjan
Bipin Maharjan

Reputation: 523

the main difference in include and header is that the include does not change the url but header does. That means header sends you (redirects you) to that page but include fetch the page for you.

see this example:
this is from test.php which is including file from test2.php this is from test.php which is including file from test2.php

this is from test1.php using header so it redirects me to test2.php
this is from test1.php using header so it redirects me to test2.php

Upvotes: -1

Daan
Daan

Reputation: 1879

Header forwards the user to a new page, so PHP reinitializes, it's like a HTML meta redirection, but faster.

Include just includes the file where you call it, and it executes it as PHP, just like if the code from homepage.php was written where you write <?php include('homepage.php'); ?>.

Upvotes: 10

Bingy
Bingy

Reputation: 644

NOTE:

the header location will eb a location that is readable by the web browser... and not the directory structure. (which include does)

also the include method will not change the page that the browser is pointing at.

Upvotes: 0

Zaje
Zaje

Reputation: 2309

The first is used for redirecting users to a different page.

Second is mostly used in templating systems to use various pages in one page. eg header.php, and footer.php will be included in content.php.

Upvotes: 0

AJ.
AJ.

Reputation: 28174

1 tells PHP to send a Location header to the HTTP client, forcing a redirect to "homepage.php".

2 tells PHP to include "homepage.php" inline to execution of the current page.


As a note about your question, your confusion might be over the term "header". It is overloaded sometimes to refer to the top part of a page in reference to code separation. Code separation is a common practice where one puts PHP code/HTML used in multiple pages into a separate file, and then included in the top (header) of each page.

HTH,

-aj

Upvotes: 2

Matt Ellen
Matt Ellen

Reputation: 11592

The header function is used to send raw HTTP headers back to the client: PHP header function

<?php
header("HTTP/1.0 404 Not Found");
?>

The above (taken from the PHP documentation) sends a 404 header back to the client.

The include function is used to include files into the current PHP script (the same as require) PHP include function

vars.php

<?php
$color = 'green';
$fruit = 'apple';
?>

test.php

<?php
echo "A $color $fruit"; // A
include 'vars.php';
echo "A $color $fruit"; // A green apple
?>

This example (again from the PHP documentation) includes the vars.php script in the test.php script and, after the include, allows the test.php script to access the variables declared in the vars.php script.

Upvotes: 6

Josh K
Josh K

Reputation: 28883

The first tells the browser to send a header to the browser to redirect to "homepage_php" (should be .?)

The second includes the file at the top. This is useful if you're using methods or classes stored in other files, or want the same content to appear on multiple pages.

Upvotes: 0

Mike Sherov
Mike Sherov

Reputation: 13427

Header redirects the browser. Include tells php to include the contents of a file and execute it as PHP.

Upvotes: 0

Related Questions