PHP File-Inclusion

Learn different methods on how to include a file in a PHP script.

php file inclusion

Up to this point, the php scripts we’ve written in this course are fairly simple and short. But, real world project is different. When you develop a large website, you’ll find that same codes are being used again and again in different scripts. For example, most pages in a website have common header, footer, and navigation menus. In this case, should you copy and paste the same header code in every page?

Of course not. You can write header code once in a separate script and include it in required web pages. PHP has a feature named server-side includes that can incorporate code of a script into another script.

PHP has four functions to incorporate external files-

  1. include()
  2. include_once()
  3. require()
  4. require_once()

include():

  • If you include a file in the current script, it is same as if pasted the content of the included file in that position.

Syntax:
include(‘./path/to/the/filename’);

  • The parentheses are optional. So, the following is also correct.
    include ‘/path/to/the/filename’;
  • To refer a relative path, it is recommended to use ./ in the beginning to ensure that the path begins from the current location.
  • You can use double or single quotes.

Example:
main.php contains the following.

<?php
echo "Hi, this line is in main.php.";
     include "./abc.php";
?>

abc.php contains the following.

<?php
     echo "<br />";
     echo "Hello, This line is in the abc.php.";
?>

Output:
Hi, this line is in main.php.
Hello, This line is in the abc.php.

Explanation:
When you run the main.php file, you’ll see the above output. The first line of the output is the outcome of the first line. The second line “Hello, This line is in the abc.php.” comes from the included file abc.php file which is included in the second line of the index.php script.

require():

require works just like as include(). So all the above information for about include is also true for require().

Difference between include() and require()

When the main script fails to include the specified file, include() shows a warning and continue executing the rest of the page. On the other hand, require() shows a warning and stops continuing.

Example:
In the two examples below, we’ll use include() in the first example and require() in the second example.

main.php contains the following.

<?php
     echo "Hi, this line is in main.php.";
     include "./abcd.php";
     echo “This is another line in main.php”;
?>

Output:
Hi, this line is in main.php
Warning: include(./abcd.php): failed to open stream: No such file or directory in F:\xampp\htdocs\ main.php on line 3
This is another line in main.php

Now, the same main.php file that contains require() in place of include().

<?php
     echo "Hi, this line is in main.php.";
     require "./abcd.php";
     echo “This is another line in main.php”;
?>

Output:
Hi, this line is in main.php
Warning: require(./abcd.php): failed to open stream: No such file or directory in F:\xampp\htdocs\ main.php on line 3

Explanation:
In the first example, include() continues executing after displaying the warning and outputs “This is another line in main.php”. In the next example, require() stops working after the warning.

When to use include() and require()?

  1. If the main script is unable to run without the specified script, then you can use require(). For example, main script might call a function or a class that is written in the specified script.
  2. Otherwise, you can always use include(). As this will display information that may help user. For example, for any reason if header is failed to included, but the remaining content of the main script might be helpful to display.

include_once():

include_once() works similarly to include() except it checks at first whether the file has already been included. Sometimes accidentally same file can be called more than once. If include_once() finds the file is already existed, it will not include again.

Syntax:
include_once(‘./path/to/the/filename’);

include_once() follows all the rules that include() has.

After include_once() is declared any subsequent attempts to include the same file using include() or include_once() will be ignored.

require_once()

require_once() works similarly to require() except it checks at first whether the file is already included. Sometimes accidentally same file can be called more than once. If require_once() finds the file is already existed, it will not include again.

Syntax:
require_once(‘./path/to/the/filename’);

require_once() follows all the rules that require() has.

After require_once() is declared any subsequent attempts to include the same file using require() or require_once() will be ignored.

Advantage of using reusing code

Time: The bigger the size of the website, the more time it will take to develop. If there is provision to use reusing code, it will significantly reduce development time.

Cost: Reusing code means less number of codes which result less testing, less maintaining, less modifying, less updating. In a whole, the cost reduces.

Reliability: It is very common that the external file that is incorporated is being used for many days and therefore the codes inside are bug-free and tested. If you write new code instead of reusing the tested code, it has more probability to have bug in it.

‹‹ PHP Loops : Previous Lesson Next Lesson: PHP Functions ››


Pin It

↑ Back to Top