Monday 10 March 2014

Include and require function in php


php include require
Include and Require Function in php programming language. You may see websites on internet and think how you will get design and templates for every page the answer is there is preset template not necessary using include or require you must be using very little bit but there is some kind of way to include this suppose i had two files such as index.php and header.inc.php. Now header.inc.php had html tag <h1> my page </h1> now if i want to create multiple pages with same heading

Code for include function :-

FILE NAME :- index.php (Screenshot1)


include functions php
Screenshot1 Index.php


<?php



include 'header.inc.php';



$var = 'String';



echo $var;



?>

FILE NAME :- anotherpage.php (Screenshot2)


header function in php
Screenshot2 Anotherpage.php


<?php



include 'header.inc.php';



$var = 10;



echo $var;



?>

FILE NAME :- header.inc.php (Screenshot3)


function in php
Screenshot3 Header.inc.php

<h1>My Page </h1>

Output (Screenshot4 and screenshot 5) :-

functions php
screenshot4 Output of index.php

header functions
Screenshot5 Output of anotherpage.php


In this example i had created three files index.php , anotherpage.php and header.inc.php. here header.inc.php is my template file. In index.php i had created variable string and just echo out very simple then in file anotherpage.php taken variable 10 and echo out. Now if i want header My page on both of the page i will use either include function or require function but now for this purpose i will use include function then later i will explain where we will use require function. So, we do include with either ” or “” you also put in brackets like (”) but i just prefer with ”. if its in another directory i would write ../header.inc.php and if it is in folder i would write folder/header.inc.php. Now here its in same folder with php file so simply header.inc.php. Now i had included my header.inc.php with index.php and anotherpage.php with simply include keyword.The reason we do this in general we creating website if we want to change header file we will change in templates and changes will come to all pages. If we use include in php we had to change once will if you use heading on every pages we had to change at every pages of over websites.We can also make footers, body, sidebar widgets, etc from include keyword. You can use many include functions on one page

Code for include function (Screenshot6) :-

include function
Code for Include functions Screenshot6


<?php



include 'doesntexist.php';



echo 'var1 is '.$var1;



?>


Output (Screenshot7) :-

function include
Screenshot7
In this example you can see i had include keyword it will show error because page doesntexist.php not found and also variable var1 is not declare then also its echo out var1 is where in require keyword when its error in page it will not execute future page or future code let se same example for require.

code for require function (Screenshot 8) :-


functions require
Screenshot8

<?Php



require 'doesntexist.php';



echo 'var1 is '.$var1;



?>

Output (Screenshot 9):-


function require
Screenshot9
Hence include and require functions do same work in php both include pages inside another page of php but difference if require keyword find any error on page it will kill future page will in include it execute future after showing error as you seen examples of include keyword after error you can see var1 is and variable is not there because file doesntexist will in require example it will not executing further after error it does not show var1 is because it kills page after error occurs.
Require is used to when you may use lots of variable to include require just sure that each variable does exists.
Include keyword is used for design base purposes for example you may want to include menu in your website.
There are two more functions same as require and include which are require_once and include_once it will just check whether you call same page at one time if it will already included it will not include again that page



No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates