Include and include_once statements in PHP : Tutorial


Include is one of the very useful statement in PHP. We can include contents of php files into other php file using include statements.



Syntax:
include 'FILE_PATH';

Example 1:
-> file_A.php 

 <?php
 echo 'Hello there!<br>';
 ?>


-> file_B.html

My name is <b>Mike</b>.



-> IncludeExample.php

<?php

 include 'file_A.php';
 include '../../file_B.html';
 echo "I love PHP!";

?>
Output

Hello there! My name is Mike. I love PHP!
In the above example,
Contents of external files file_A.php and file_B.html are included and evaluated into IncludeExample.php file before it is executed at the server end.
Remember to provide correct path to the files included.
Note that we can also include HTML or files with any other extensions.
Some of the usage of Include statatements are,

1. Include header's and footer's to pages.
1. Include navigation menu's to pages.
2. Holding variable's that are used across pages
3. Designing layouts for websites
4. Including dynamically generated contents


include_path

We can define the include file path using include_path in configuration file php.ini

Syntax
include_path = string;


include_path example for Unix hosting
include_path = ".:/dir1/dir2";


include_path example for Windows hosting
include_path = ".;c:\dir1\dir2";



Example 2: include statement embedded in HTML
-> file_A.php 

 <?php
 echo 'Hello there!<br>';
 ?>



-> IncludeExample2.php

<html>
<head>
 <body>
   <?php include 'fileA.php'; ?>  
 </body>
</html>

Output

Hello there!


If include file path is incorrect or file is not found, an warning message is displayed saying that it cannot find a file(E_WARNING) and the script will continue.

Example 3: include statement file not found warning
-> file_A.php 

 <?php
 echo 'Hello there!<br>';
 ?>



-> IncludeExample3.php

<html>
<head>
 <body>
   <?php include '../fileA.php'; ?>  
   <?php echo "<br>This will still be printed!!" ?> 
 </body>
</html>

Output

Warning: include(../fileA.php): failed to open stream: No such file or directory in I:\xampp\htdocs\xampp\Tutorials\IncludeExample3.php on line 5 Warning: include(): Failed opening '../fileA.php' for inclusion include_path='.;I:\xampp\php\PEAR') in I:\xampp\htdocs\xampp\Tutorials\IncludeExample3.php on line 5 This will still be printed!!
As the warning message is displayed on the Console (or webpage) this may reveal files name a path details causing an security issue.

include_once Statement

include_once statement is same as include, the only difference is that if the file has already been included, it will not be included again.
include_once should be used when a same file might be included and evaluated more than once during a particular execution of a script. It will help to avoid problems such as function redefinitions, variable value reassignments, etc.
Example 4: include_once
-> file_A.php 

 <?php
 echo 'Hello there!<br>';
 ?>

-> IncludeExample.php

<?php

 include_once 'file_A.php'; //file_A.php will be included.
 include_once 'file_A.php'; //As file_A.php is included 
 //above (using include_once) it wont be included again.

 echo "I love PHP!";

?>
Output

Hello there! I love PHP!
Note : "Hello there!" is printed only once!