Require and require_once statements in PHP : Tutorial


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

Require works same as Include statements, the only difference is, require throws a fatal error and stop the script execution.

Hence it is good to use require instead of include to avoid security issues.

Syntax:
require 'FILE_PATH';

Example 1:
-> file_A.php 

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


-> file_B.html

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



-> IncludeExample.php

<?php

 require 'file_A.php';
 require '../../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 Require 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: require statement embedded in HTML
-> file_A.php 

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



-> IncludeExample2.php

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

Output

Hello there!


If require file path is incorrect or file is not found, an Fatal Error 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 require '../fileA.php'; ?>  
   <?php echo "<br>This will NOT 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 Fatal error: require(): Failed opening required '../a.php' (include_path='.;I:\xampp\php\PEAR') in I:\xampp\htdocs\xampp\Tutorials\IncludeExample3.php on line 5
A Fatal error is displayed on the Console (or webpage) and the script execution is halted (Note that the echo message after requice statement is not displayed)

require_once Statement

require_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!