PHP Include Files

using php include functionality - you can include the content of a php file into another php file before the server executes it. php provide a include function which can be used to included one php file into another php file.


         include(absolute_path/ relative_path)
  • absolute_path :- path starting with a drive letter (\ for window / for linux/unix)
  • relative_path :- path starting with . or .. (e.g ..\header.php)

the complete information about the path:

note : include() function, if not parsed correctly, will continue processing the rest of the page and display a warning (e_warning ) in the page where the included file should appear.

include files can make sites easier to manage. this means that you can create a standard header part, footer, navigation for all your web pages. when the footer header needs to be updated, you can only update the header, footer include file.

in this example below we first create header file (header.php)

   <div  style = "background-color:gold;width:800px;height:100px;">
           <li><a href="" target="_blank">google</a></li>
           <li><a href=""  target="_blank">yahoo</a></li>
           <li><a href=""  target="_blank">facebook</a></li>
           </ul>  </div>

in this example below we create footer.php file.

   <div style = "background-color:#efeffe;color:#3a4b9c;font-weight:bold;">
            <p>advertising programs</p>
            <p>business solutions</p>

all pages in the web site should include this header.php and footer.php files. here is how it can be done:

    <head> <title>demo for php include files </title> </head>
     <?php  include("header.php");  ?>
 <h1>this is the first heading </h1>
 <h2>this is the second heading</h2>
 <h3>this is the third  heading</h3>                     

what if we include file with some variables defined ('vari.php');

   <?php     $name ="sandeep kumar nehra";

                $age  ="25"; ?>

then the variables can be used in the calling file:

  <html>  <body>
       <h2> your information <\h2>
       <?php    include('vari.php');  
                   echo "my name is = $name";
                   echo "<br/> age = $age";
/ if the include occurs inside a function within the calling file, then all of the code contained 

in the called file will behave as though it had been defined that if will follow the 

variable scope of that function./          

PHP include_once() function

this function behavior similar to the include statement, but only difference being that if the code from a file has already been included, it will not be included again.



path may be absolute or relative.


  var_dump( include_once('header.php')); // file already include so output is true
  var_dump( include_once('header.php'));

include_once may be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, so in this case it may help avoid problems such as function redefinitions, variable value reassignments, etc.

you should use include_once().

PHP require function

the require is identical to include, except upon failure it will also produce a fatal E_compile_error level error.


         require_once(path )