PHP Include Function

PHP Include( ) The PHP include() statement holds all the code/text that liesin the defined file and paste it into the file that uses the "include" statement. With the help of include statement,we can insert the code/text of one PHP file into another PHP file(before server executes it). Including file saves a lot of time or work. It means that we can create a standard header, footer, body, or menu file for all web pages. When we need to update a header, footer, or any web page, we can only update the included files. Syntax:

include 'filename'
Example1:
  • We have a PHP file called "php":
<?php
  echo '<a href="#">Home</a>
  <a href="#">AboutUs</a>
  <a href="#">Gallery</a>
  <a href="#">Testimonials</a>
  <a href="#">Favorites</a>
  <a href="#">Login</a>
  <a href="#">Contact</a>';
?>
  • To include the "php" in a page, we use include statement.
<!DOCTYPE html>
<html>
<body>
<h4>IncludeExample</h4>
<?phpinclude 'nav.php';?>
</body>
</html>
Output: IncludeExample
Home AboutUs Gallery Testimonials Favorites Login Contact
Example2:
  • We have a PHP file called "variable.php" with some defined variables:
<?php
$animal='Lion';
$flower='Lily';
$fruit='Strawberry';
?>
  • We include the PHP file "variable.php", in the calling file:
<!DOCTYPE html>
<html>
<body>
<h4>IncludeVariableExample</h4>
<?php include 'variable.php';
echo "$animal $flower $fruit."
?>
</body>
</html>

Difference between PHP include and require statements:

include require
1)  PHP include takes one argument, the filename that we want to include. PHP required() to perform the same process as include() does, takes one argument, the filename that we want to require.
2) Syntax: include 'filename'; example: include 'menu.php'; Syntax: require 'filename'; example:  require 'menu.php';
3)  PHP include() does not stop execution. PHP require() does stop execution once it gets a fatal error.
4) PHP include() gives the warning,and it continues to execute the script, in case if filename not found. If PHP require()found any problem like file not found or misnamed files and stops the execution once get a fatal error.
5) We can use PHP include() when the file is not required or not so important. When the script requires the file, it's better to use required().
Example3:
<!DOCTYPE html>
<html>
<body>
<h4>Included_File_Not_Exists</h4>
<?php include 'filenotexists.php';
echo "IncludeExample."
?>
</body>
</html>
Output:

Included_File_Not_Exists

Warning: include(filenotexists.php): failed to open stream: No such file or directory in C:\xampp\htdocs\inc.php on line 5 IncludeExample. In the above example, a file (filenotexists) is included with the include statement and PHP cannot find it, the PHP script will continue to execute. Example 4:
<!DOCTYPE html>
<html>
<body>
<h4>Require_File_Not_Exists</h4>
<?php require 'filenotexists.php';
echo "RequireExample."
?>
</body>
</html>
Output:

Require_File_Not_Exists

Fatal error: require(): Failed opening required 'filenotexists.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\req.php on line 5 In the above example, the echo statement will not be executed because the script execution dies after the require statement returned the fatal error.
  • PHP include_once:
The file will be included just once. This statement is similar to the include statement, except only one difference that if the file has been already included, it will not beincluded again, it returns TRUE.
  • PHP require_once:
The require_once statement is similar to require statement except PHP file has already been included and if so, not require it again.