Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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.

like image 316
Deepak Narwal Avatar asked Jan 31 '10 22:01

Deepak Narwal


2 Answers

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.

like image 127
Matt Ellen Avatar answered Nov 20 '22 18:11

Matt Ellen


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'); ?>.

like image 10
Daan Avatar answered Nov 20 '22 17:11

Daan