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)
Screenshot1 Index.php |
<?php include 'header.inc.php'; $var = 'String'; echo $var; ?>
FILE NAME :- anotherpage.php (Screenshot2)
Screenshot2 Anotherpage.php |
<?php include 'header.inc.php'; $var = 10; echo $var; ?>
Screenshot3 Header.inc.php |
Output (Screenshot4 and screenshot 5) :-
screenshot4 Output of index.php |
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) :-
Code for Include functions Screenshot6 |
<?php include 'doesntexist.php'; echo 'var1 is '.$var1; ?>
Output (Screenshot7) :-
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) :-
Screenshot8 |
<?Php require 'doesntexist.php'; echo 'var1 is '.$var1; ?>
Output (Screenshot 9):-
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..