Monday, 16 November 2015

8_include_once() and require_once()

include_once() and require_once()


consider the header.inc.php file from previous session…


Now write the code in page3.php



< ? php
require_once(“header.inc.php”);
require(“header.inc.php”);
? >


When you run the page3.php in browser it will display the message two time


O/P:---


Welcome To Our Site


Welcome To Our Site


WHY ? ? ? ? ? ?


require_once() finction will add/include the header.inc.php file in page3.php and require() function also add/include the header.inc.php


And in page3.php we want to add/include header.inc.php file ONLY ONCE


So what we will do now ?????



< ? Php
require(“header.inc.php”);
require_once(“header.inc.php”);
? >


Change the little bit line of code from page3.page as I have done above…


You can see that first I have use require() and second I have used require_once()


Now run the page3.php in browser…. YPEEEEEEEE


You can see that “Welcome to Our Site” Message ONLY ONCE


So here require_once function try add/include the header.inc.php file in page3.php but it it already added using require()… So if file already added then require_once function will NOT ADD the header.inc.php file AGAIN IN PAGE3.php page.



No comments:

Post a Comment