PHP include & require

In PHP, the include() and require() statement allows to reuse the content of a php, html or text file on the multiple pages of a website or web application. These both statements are basically takes all code/text or markup that exists in the specified file and copies it into the file that uses these statements.

The main difference between include() and require() is that when a file is included with include() statement and the included file can not be found, then the include() statement will only generate a PHP warning and allow the execution of php script, but the require() statement will generate a fatal error and stops the script execution.

include() :

The include() statement is used to include the code contained in a PHP file to another PHP file. And if the included file does not exists, then it only produces a warning and the php script will continue to execute. The basic syntax of include() statement is :
 include("name of the file with path");
or
 include "name of the file with path";
Example :
 include ("file.php");
or
 include "file.php";
Now lets see an example, suppose we have three php files :

header.php
<html>
<body>
  
  <div style="font-size: 40px;text-align: center;width: 100%;background-color:yellow;font-weight: bold;">
  <?php
    echo "This is title of the Page.";
  ?>
  </div>

  <div style="font-size: 20px;text-align: center;">
  <?php
    echo "| <a href='www.mycsnotes.com'>Tab1</a> | ";
    echo "| <a href='www.mycsnotes.com'>Tab2</a> | ";
    echo "| <a href='www.mycsnotes.com'>Tab3</a> | ";
    echo "| <a href='www.mycsnotes.com'>Tab4</a> | ";
    echo "| <a href='www.mycsnotes.com'>Tab5</a> | ";
  ?>
  </div>
footer.php
  <div style="bottom: 10;text-align: center;">

  <?php
    echo "<b>Copyright 2017-" . date("Y") . " www.mycsnotes.com</b>";
  ?>
  </div>

</body>
</html>
content.php
  <?php
    include ("header.php");
  ?>

  <div style="height:250px;">
    <p><b>Hello world This is The Content Of The Page.</b></p>
    PHP is a widely-used open source general-purpose scripting language. It is an HTML-embedded, server-side scripting language 
    primarily used to develop static and dynamic websites and web applications. PHP is very powerful and scalable language yet 
    easy to learn and use.</p>

    <p>The PHP stands for "PHP : Hypertext Preprocessor", where the first PHP meas Personal Home Page. PHP is an interpreted
    language, means theres no need for any compilation, we just only need to install php application on the server. 
    PHP code are executed on the server, and the result is returned to the browser as plain HTML. PHP files can contain 
    text, HTML, CSS, JavaScript, and PHP code. It has ".php" file extension. The server interprets the PHP code and outputs 
    the results as HTML code to the web browsers. PHP support Object Oriented Programming.</p>
  </div>

  <?php
    include ("footer.php");
  ?>
Output :


At the above example, the content.php includes header.php and footer.php, where the page titles, tab menu and footer defined.

 include_once() : If we accidentally include the same file more then one time by using include statements, then it may cause conflicts. To prevent this situation, the include_once() function is used. The include_once() function will include the file only once, and if the file has been already included within the previous statement, then it not include it again.

func.php
<?php
  
  function addNum($x, $y) {
    return ($x+$y);
  }

?>
test.php
<?php

  include_once("func.php");

  echo "Adding two numbers : 20 and 30 <br/>";
  echo "Answer : " . addNum(20, 30);

?>
Output :


require() :

The require() statement is also used to include the code contained in a PHP file to another PHP file. But the main difference between include() and require() is that in require() statement if the included file does not exists, then it produce a fatal error and stop the execution of php script.The syntax of require() statement is:
 require("name of the file with path");
or
 require "name of the file with path";
Example :
 require ("file.php");
or
 require "file.php";
Example :
<?php

  require("header.php");

?>

<div style="height:250px;">

  <p><b>Hello world This is The Content Of The Page.</b></p>
  PHP is a widely-used open source general-purpose scripting language. It is an HTML-embedded, server-side scripting language 
  primarily used to develop static and dynamic websites and web applications. PHP is very powerful and scalable language yet 
  easy to learn and use.</p>

  <p>The PHP stands for "PHP : Hypertext Preprocessor", where the first PHP meas Personal Home Page. PHP is an interpreted
  language, means theres no need for any compilation, we just only need to install php application on the server. 
  PHP code are executed on the server, and the result is returned to the browser as plain HTML. PHP files can contain 
  text, HTML, CSS, JavaScript, and PHP code. It has ".php" file extension. The server interprets the PHP code and outputs 
  the results as HTML code to the web browsers. PHP support Object Oriented Programming.</p>

</div>

<?php

   require("footer.php");

?>
Output :


But within require() statement if the included file is not found then it will display an error. For example :
<?php

  require("addfunc.php");

  echo "Adding two numbers : 20 and 30 <br/>";
  echo "Answer : " . addNum(20, 30);

?>
Output :


require_once() : The require_once() statement include the code contained in a PHP file to another PHP file only once, means if the file has been already included within the previous statement, then it not include it again. Because if we try to include a file twice with require() statement, then it will cause an error.

Example :

func.php
<?php

  function addNum($x, $y) {
    return ($x+$y);
  }

?>
test.php
<?php

  include_once("func.php");

  echo "Adding two numbers : 20 and 30 <br/>";
  echo "Answer : " . addNum(20, 30);

?>
Output :

test

Next Topic :