Monday, 16 November 2015

7_Include and Require

Include and Require


Create 3 Page Page1.php , Page2.php And header.inc.php


In Page header.inc.php page… Write the code like “ WELCOME TO OUR SITE ”


and I want to use this line to display the message in page1.php and page2.php page … what we suppose to do…


In Page1.php write the code like include(“header.inc.php”);


Same way in page2.php write the code like include(“header.inc.php”);


So what will happen is Page1 and Page 2 both will display the message “WELCOME TO OUT SITE” from header.inc.php page.


Used in the template design page… And also used to assign the global to the variable


header.inc.php
< ? php echo “Welcome to Out Site”; $var1=10; $var2=”HARSHIDA”; ? >


As you can see that $var1 and $var2 both variable has assign value in header page. So both the value can access by page1 and page2. Means In page page1 and page2 we are allowed to use the value of this variable.


Now if we want to KILL THE PAGE IF WE ARE UNABLE TO FIND THE header.inc.php page… Then


So change the code in page1 and page2


Page1.php and page2.php
< ? Php
require(“header.inc.php”); //instead of include write require
//in page1.php
echo $var1;
//in page2.php
echo $var2;
? >


The function “require(),” Will compulsory add the page”header.inc.php” in page1.php and page2.php And if require function unable to add header.inc.php in page the it will kill the page… It will stop the execution…


While function ”include”, instead of “require()” write “include()”, if include function unable to find the header.inc.php file let’s take another example in below


Page1.php and page2.php
< ? Php
include(“hello.inc.php”);
//in page1.php
echo “The value of Variable 1 Is:==>” .$var1;
//in page2.php
echo “The value of Variable 2 is:==>”.$var2;
? >



we have included hello.inc.php file… and this is not exist in current directory… then still php server will continue it’s execution… script will execute…



No comments:

Post a Comment