Saturday, 11 June 2016

PHP include() and require() FUNCTIONS

00:20

PHP include and require Statements   :
It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with  the   include or require statement.
The  include and require statements are identical, except upon failure:
  • require will produce a fatal error (E_COMPILE_ERROR) and stop the script
  • include will only produce a warning (E_WARNING) and the script will continue

The  include() Function :
The include() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the  include() function generates a warning but the script will continue execution.
Assume you want to create a common menu for your website. Then create a file menu.php with the following content.
<a href="http://www.phpdevelopmenttricks.blogspot.com/index.htm">Home</a> -
<a href="http://www.phpdevelopmenttricks.blogspot.com/ebxml">ebXML</a> -
<a href="http://www.phpdevelopmenttricks.blogspot.com/ajax">AJAX</a> -
<a href="http://www.phpdevelopmenttricks.blogspot.com/perl">PERL</a> <br />
Now create as many pages as you like and include this file to create header. For example now your test.php file can have following content.
<html>
   <body>
  
      <?php include("menu.php"); ?>
      <p>This is an example to show how to include PHP file!</p>
     
   </body>
</html>


The   require() Function :

 

The   require() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the  require() function generates a fatal error and halt the execution of the script.
So there is no difference in require() and include() except they handle error conditions. It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.
You can try using above example with require() function and it will generate same result. But if you will try following two examples where file does not exist then you will get different results.
<html>
   <body>
   
      <?php include("xxmenu.php"); ?>
      <p>This is an example to show how to include wrong PHP file!</p>
      
   </body>
</html>
This will produce the following result −
This is an example to show how to include wrong PHP file!
Now lets try same example with require() function.
<html>
   <body>
       
       <?php require("xxmenu.php"); ?>
       <p>This is an example to show how to include wrong PHP file!</p>
   
   </body>
</html>
This time file execution halts and nothing is displayed.


 

Difference between   include     &     include_once    or     require    &   require_once:

 

The difference include vs. include_once and require vs. require_once is obvious from the names. include and require allow you to insert a file multiple times within a single execution lifetime. On the other hand,include_once and require_once make sure one file is inserted only once in a single execution lifetime, even if your CODE calls them multiple times.

 

 Difference between include  &  require  or  include_once   &  require_once:

 

Another significant difference is: include vs. require or include_once vs. require_once. Even though naming of these constructs are almost perfect according to their functionality, many PHP programmers fail to understand the significant difference.
As the name suggests, with  include or include_once, you simply include other files in the flow of execution. On the other hand, when you use  require or require_once, you not only include the file in the flow of execution, but also you tell PHP that the execution cannot be continued without that file.

 

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

2 comments:

  1. I am very glad to read your informative blog...thanks a lot for your valuable sharing
    you can also visit here PHP training in delhi

    ReplyDelete

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top